`
conquer0
  • 浏览: 76575 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

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

阅读更多

/**
* 缓存操作实现类.
*/
@Service("cassCache")
public class CassCache implements ICassCache
{
     /** 根据Key从缓存删除缓存数据.
     * @param aKeyArea Key
     */
    public void remove(String aKeyArea) throws ApplicationException
    {
        CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
        // 取得客户端(群集服务器)
        CassandraClient client = null;
        try
        {
            // 从群集服务器获得一个客户端
            client = pool.borrowClient(mCassandraClient);
            // Keyspace
            Keyspace keyspace =
                client.getKeyspace(mDefaultKeyspace);
            // ColumnPath
            ColumnPath columnPath = new ColumnPath(mDefaultColumnFamily);
            // 从缓存中清除
            keyspace.remove(aKeyArea, columnPath);
            // 为确保finally中能正确释放client,此处需重获取一次
            client = keyspace.getClient();
        }
        catch (Exception e)
        {
            sLog.error("根据Key从缓存删除缓存数据时出错。");
            sLog.error(e);
            throw new ApplicationException(e);
        }
        finally
        {
            // finally中释放client
            releaseClient(pool, client);
        }
    }

    /**根据Key和column从缓存删除缓存数据.
     * @param aKeyArea Key
     * @param aName column
     */
    public void remove(String aKeyArea, String aName)
        throws ApplicationException
    {
        CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
        // 取得客户端(群集服务器)
        CassandraClient client = null;
        try
        {
            // 从群集服务器获得一个客户端
            client = pool.borrowClient(mCassandraClient);
            // Keyspace
            Keyspace keyspace =
                client.getKeyspace(mDefaultKeyspace);
            // ColumnPath
            ColumnPath columnPath = new ColumnPath(mDefaultColumnFamily);
            columnPath.setColumn(bytes(aName));
            // 从缓存中清除
            keyspace.remove(aKeyArea, columnPath);
            // 为确保finally中能正确释放client,此处需重获取一次
            client = keyspace.getClient();
        }
        catch (Exception e)
        {
            sLog.error("根据Key和column从缓存删除缓存数据时出错。");
            sLog.error(e);
            throw new ApplicationException(e);
        }
        finally
        {
            // finally中释放client
            releaseClient(pool, client);
        }
    }

    /** 根据属性名从绑定中取得属性值.
     * @param aPropertyName 属性名
     * @return 从绑定中取得的属性值
     */
    public String getPropertyFromBinding(String aPropertyName)
        throws ApplicationException
    {
        return CacheConfigReader.getInstance()
                .getPropertyFromBinding(aPropertyName);
    }

    /** 释放client.
     * @param aPool CassandraClientPool
     * @param aClient Cassandra客户端
     */
    private void releaseClient(CassandraClientPool aPool,
        CassandraClient aClient)
        throws ApplicationException
    {
        try
        {
            // 释放client
            aPool.releaseClient(aClient);
        }
        catch (Exception e)
        {
            sLog.error("释放client时出错。");
            sLog.error(e);
            throw new ApplicationException(e);
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics