#include <time.h>
void main()
{
char logtime[80];
time_t now_time;
now_time=time(NULL);
strftime(logtime,sizeof(logtime),"日期:20%y-%m-%d 时间:%H:%M:%S.%M",
localtime(&now_time));
printf("%s\n",logtime);
}
EhCache event listener for preventing caching of null values in data store(ehcache监听器,坚挺value为null时清除)
ehcache的配置 <cache name="myNonNullCache" <!-- cache configuration --> > <cacheEventListenerFactory class="com.example.cache.NotNullCacheEventListenerFactory" /> </cache> 监听工厂类 package com.pzoom.crabman.store; /** * Created with IntelliJ IDEA. * User: Administrator * Date: 13-1 -7 * Time: 上午10:33 * To change this template use File | Settings | File Templates. */ import java.util.Properties; import net.sf.ehcache.event.CacheEventListener; import net.sf.ehcache.event.CacheEventListenerFactory; public class NotNullCacheEventListenerFactory extends CacheEventListenerFactory { @Override public CacheEventListener createCacheEventListener( final Properties properties) { return NotNullCacheEventListener.INSTANCE; } } 监听实现类 package com.pzoom.crabman.store; /** * Created with IntelliJ IDEA. * User: Administrator * Date: 13-1 -7 * Time: 上午10:32 * To change this template use File | Settings | File Templates. */ import net.sf.ehcache.CacheException; import net.sf.ehcache.Ehcache; import net.sf.ehcache.Element; import net.sf.ehcache.event.CacheEventListener; public class NotNullCacheEventListener implements CacheEventListener { public static final CacheEventListener INSTANCE = new NotNullCacheEventListener(); @Override public void notifyElementRemoved(final Ehcache cache, final Element element) throws CacheException { } @Override public void notifyElementPut(final Ehcache cache, final Element element) throws CacheException { removeIfNull(cache, element); } @Override public void notifyElementUpdated(final Ehcache cache, final Element element) throws CacheException { removeIfNull(cache, element); } private void removeIfNull(final Ehcache cache, final Element element) { if (element.getObjectValue() == null) { cache.remove(element.getKey()); } } @Override public void notifyElementExpired(final Ehcache cache, final Element element) { } @Override public void notifyElementEvicted(final Ehcache cache, final Element element) { } @Override public void notifyRemoveAll(final Ehcache cache) { } @Override public void dispose() { } @Override public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException("Singleton instance"); } }
近期因为嵌入式系统上面要用到数据库方面的东西,对于数据库,我还是第一次接触,之前保存数据要不就是ini文件,要不就是xml;全新的挑战啊;
查阅资料,得知SQLite是一个非常不错的轻量级数据库,下载,就一个源文件,感觉老外水平就是牛,C语言运用炉火纯青,赞一个!
当然得感谢网友的无私奉献,贡献了很多可以借鉴的代码以及经验,经过修改,刚开始写1000条数据消耗的时间大概是21秒,感觉速度有点慢,
觉得不对劲,继续查阅相关资料,发现有这么个说法:
没有事务的时候,SQLite的插入操作使用了太多的IO操作,而是用事务的话,只需要一次IO。(http://www.cnblogs.com/rader/articles/1543760.html)
于是果断找资料,原来sqlite,每次insert的时候都会处理一次事务,那么3W多条数据,也就3W多事务处理,就这样悲剧了.(http://www.2cto.com/database/201208/146785.html)
修改后的代码如下:
struct timeval tv, tvEnd;
int i;
sqlite3_exec( db, "BEGIN", 0, 0, 0);
gettimeofday( &tv, NULL );
printf("Start %d %d \n", tv.tv_sec, tv.tv_usec/1000);
for ( i=0; i<100000; i++ )
{
result = sqlite3_exec( db, "insert into MyTable_1( name ) values ( 'abcdefghigh' )", 0, 0, &errmsg );
if(result != SQLITE_OK )
{
printf( "插入记录失败,错误码:%d,错误原因:%s\n", result, errmsg ); //不好意思,网上拷贝人家的代码,中文;
}
}
sqlite3_exec( db, "COMMIT", 0, 0, 0);
效率如下:
Use 0 sec 739 ms
10万条记录才花7.4ms,效率确实很高啊!