cassandra内部API使用二2010-07-07

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

*@paramaKeyAreaKey

*@paramaNamecolumn

*@returnObject缓存数据

*/

publicObjectget(StringaKeyArea,StringaName)

{

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)

{

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);

}

catch(Exceptione)

{

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

sLog.error(e);

}

finally

{

//finally中释放client

releaseClient(pool,client);

}

returnnull;

}

相关推荐