缓存cassandra六(缓存操作实现类)续五

/**

*缓存操作实现类.

*/

@Service("cassCache")

publicclassCassCacheimplementsICassCache

{

/**根据Key和column从缓存读取数据.

*@paramaKeyAreaKey

*@paramaNamecolumn

*@returnObject缓存数据

*@throwsApplicationExceptionApplicationException

*/

publicObjectget(StringaKeyArea,StringaName)throwsApplicationException

{

Map<String,Object>resultMap=getResultMap(aKeyArea,aName);

if(resultMap!=null)

{

returnresultMap.get(CacheConstants.CACHE_MAP_KEY_VALUE);

}

returnnull;

}

/**根据Key和column从缓存读取数据,包括timestamp.

*@paramaKeyAreaKey

*@paramaNamecolumn

*@returnMap<String,Object>缓存数据

*key:"cache.timestamp",value:存放缓存数据时的timestamp

*key:"cache.value",value:缓存对象

*/

publicMap<String,Object>getResultMap(StringaKeyArea,StringaName)

throwsApplicationException

{

Map<String,Object>resultMap=newHashMap<String,Object>();

CassandraClientPoolpool=CassandraClientPoolFactory.INSTANCE.get();

//取得客户端(群集服务器)

CassandraClientclient=null;

try

{

//从群集服务器获得一个客户端

client=pool.borrowClient(mCassandraClient);

//Keyspace

Keyspacekeyspace=

client.getKeyspace(mDefaultKeyspace);

//ColumnPath

ColumnPathcolumnPath=newColumnPath(mDefaultColumnFamily);

columnPath.setColumn(bytes(aName));

//从缓存读取

Columncol=keyspace.getColumn(aKeyArea,columnPath);

byte[]objBytes=col.getValue();

//由微秒转换为毫秒

longtimestamp=col.getTimestamp()

/CacheConstants.MICROSECONDS_PER_MILLISECONDS;

//从字节数组获取对象

Objectobj=ByteUtil.getObjectFromBytes(objBytes);

//缓存对象

resultMap.put(CacheConstants.CACHE_MAP_KEY_VALUE,obj);

//存放缓存数据时的timestamp

resultMap.put(CacheConstants.CACHE_MAP_KEY_TIMESTAMP,timestamp);

//为确保finally中能正确释放client,此处需重获取一次

client=keyspace.getClient();

//用户的缓存数据

returnresultMap;

}

//Key或者Column不存在

catch(NotFoundExceptione)

{

sLog.error(e);

returnnull;

}

catch(Exceptione)

{

sLog.error("根据Key和column从缓存读取数据(包括timestamp)时出错。");

sLog.error(e);

thrownewApplicationException(e);

}

finally

{

//finally中释放client

releaseClient(pool,client);

}

}

}

相关推荐