memcached的使用(二)hibernate cache provider

实现hibernate的cache provider,让hibernate使用memcached缓存。
这里比较简单,由于memcached的SockIOPool已经在(一)中spring中初始化了,这里就不考虑pool的初始化,直接获得MemCachedClient使用即可。
然后配置给hibernate<prop key="hibernate.cache.provider_class">×××</prop>

附代码:
[code]/**
* @author Marc
*
*/
public class MemcachedProvider implements CacheProvider {
public Cache buildCache(String name, Properties properties) throws CacheException {
return new MemCache(name);
}

public boolean isMinimalPutsEnabledByDefault() {
return false;
}

public long nextTimestamp() {
return Timestamper.next();
}

public void start(Properties properties) throws CacheException {
}

public void stop() {
}

}[/code]
[code]/**
* @author Marc
*
*/
public class MemCache implements Cache {
private static final Log log = LogFactory.getLog(MemCache.class);

private static final int SIXTY_THOUSAND_MS = 60000;

MemCachedClient mc;
String regionName;

/**
* Creates a new Hibernate pluggable cache based on a cache name. <p/>
*
* @param cache
* The underlying EhCache instance to use.
*/
public MemCache(String regionName) {
mc = new MemCachedClient();
mc.setCompressEnable(false);
this.regionName = regionName;
}

/**
* Gets a value of an element which matches the given key.
*
* @param key
* the key of the element to return.
* @return The value placed into the cache with an earlier put, or null if
* not found or expired
* @throws CacheException
*/
public Object get(Object key) throws CacheException {
if (log.isDebugEnabled()) {
log.debug("key: " + key);
}
if (key == null) {
return null;
} else {
Object rt = mc.get(key.toString());
if (rt == null) {
if (log.isDebugEnabled()) {
log.debug("Element for " + key + " is null");
}
return null;
} else {
return rt;
}
}

}

public Object read(Object key) throws CacheException {
return get(key);
}

/**
* Puts an object into the cache.
*
* @param key
* a key
* @param value
* a value
* @throws CacheException
* if the {@link CacheManager} is shutdown or another
* {@link Exception} occurs.
*/
public void update(Object key, Object value) throws CacheException {
put(key, value);
}

/**
* Puts an object into the cache.
*
* @param key
* a key
* @param value
* a value
* @throws CacheException
* if the {@link CacheManager} is shutdown or another
* {@link Exception} occurs.
*/
public void put(Object key, Object value) throws CacheException {
mc.set(key.toString(), value);
}

/**
* Removes the element which matches the key. <p/> If no element matches,
* nothing is removed and no Exception is thrown.
*
* @param key
* the key of the element to remove
* @throws CacheException
*/
public void remove(Object key) throws CacheException {
mc.delete(key.toString());
}

/**
* Remove all elements in the cache, but leave the cache in a useable state.
*
* @throws CacheException
*/
public void clear() throws CacheException {
log.warn("cann't clear all items in memcached!");
throw new CacheException("cann't clear all items in memcached!");
}

/**
* Remove the cache and make it unuseable.
*
* @throws CacheException
*/
public void destroy() throws CacheException {

}

/**
* Calls to this method should perform there own synchronization. It is
* provided for distributed caches. Because EHCache is not distributed this
* method does nothing.
*/
public void lock(Object key) throws CacheException {
}

/**
* Calls to this method should perform there own synchronization. It is
* provided for distributed caches. Because EHCache is not distributed this
* method does nothing.
*/
public void unlock(Object key) throws CacheException {
}

/**
* Gets the next timestamp;
*/
public long nextTimestamp() {
return Timestamper.next();
}

/**
* Returns the lock timeout for this cache.
*/
public int getTimeout() {
// 60 second lock timeout
return Timestamper.ONE_MS * SIXTY_THOUSAND_MS;
}

public String getRegionName() {
return this.regionName;
}

/**
* Warning: This method can be very expensive to run. Allow approximately 1
* second per 1MB of entries. Running this method could create liveness
* problems because the object lock is held for a long period <p/>
*
* @return the approximate size of memory ehcache is using for the
* MemoryStore for this cache
*/
public long getSizeInMemory() {
log.warn("cann't getSizeInMemory in memcached!");
throw new CacheException("cann't getSizeInMemory in memcached!");
}

public long getElementCountInMemory() {
log.warn("cann't getElementCountInMemory in memcached!");
throw new CacheException("cann't getElementCountInMemory in memcached!");
}

public long getElementCountOnDisk() {
log.warn("cann't getElementCountOnDisk in memcached!");
throw new CacheException("cann't getElementCountOnDisk in memcached!");
}

public Map toMap() {
log.warn("cann't toMap in memcached!");
throw new CacheException("cann't toMap in memcached!");
}

public String toString() {
return "MemCached(" + getRegionName() + ')';
}

}[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值