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

packagecom.huawei.support.cache.impl;

importstaticme.prettyprint.cassandra.utils.StringUtils.bytes;

importjava.io.Serializable;

importjava.util.HashMap;

importjava.util.Map;

importme.prettyprint.cassandra.service.CassandraClient;

importme.prettyprint.cassandra.service.CassandraClientPool;

importme.prettyprint.cassandra.service.CassandraClientPoolFactory;

importme.prettyprint.cassandra.service.Keyspace;

importorg.apache.cassandra.thrift.Column;

importorg.apache.cassandra.thrift.ColumnPath;

importorg.apache.cassandra.thrift.NotFoundException;

importorg.apache.commons.logging.Log;

importorg.apache.commons.logging.LogFactory;

importorg.springframework.stereotype.Service;

importcom.huawei.support.cache.CacheConstants;

importcom.huawei.support.cache.ICassCache;

importcom.huawei.support.utils.ByteUtil;

/**

*缓存操作实现类.

*/

@Service("cassCache")

publicclassCassCacheimplementsICassCache

{

/**

*sLog

*/

privatestaticLogsLog=LogFactory.getLog(CassCache.class);

/**

*客户端(群集服务器)

*/

privateString[]mCassandraClient;

/**

*默认Keyspace

*/

privateStringmDefaultKeyspace;

/**

*默认columnFamily

*/

privateStringmDefaultColumnFamily;

/**构造函数.

*/

publicCassCache()

{

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

mCassandraClient=CacheConfigReader.getInstance()

.getCassandraClient();

//默认Keyspace

mDefaultKeyspace=CacheConfigReader.getInstance().getDefaultKeyspace();

//默认columnFamily

mDefaultColumnFamily=CacheConfigReader.getInstance()

.getDefaultColumnFamily();

}

/**向缓存中存放数据.

*

*@paramaKeyAreaKey

*@paramaNamecolumn

*@paramaSerialObj缓存对象

*/

publicvoidput(StringaKeyArea,StringaName,SerializableaSerialObj)

{

CassandraClientPoolpool=CassandraClientPoolFactory.INSTANCE.get();

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

CassandraClientclient=null;

try

{

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

client=pool.borrowClient(mCassandraClient);

//Keyspace

Keyspacekeyspace=client.getKeyspace(mDefaultKeyspace);

//ColumnPath

ColumnPathcolumnPath=newColumnPath(mDefaultColumnFamily);

columnPath.setColumn(bytes(aName));

//从对象获取字节数组

byte[]objBytes=ByteUtil.getBytesFromObject(aSerialObj);

//向缓存中存放数据

keyspace.insert(aKeyArea,columnPath,objBytes);

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

client=keyspace.getClient();

}

catch(Exceptione)

{

sLog.error("向缓存中存放数据时出错。");

sLog.error(e);

}

finally

{

//finally中释放client

releaseClient(pool,client);

}

}

相关推荐