| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /** Copyright 2009 by primeton Corporation.
- *
- * All rights reserved.
- *
- * This software is the confidential and proprietary information of
- * primeton Corporation ('Confidential Information'). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with primeton.
- */
- package com.primeton.dgs.kernel.core.cache.impl;
- import net.rubyeye.xmemcached.MemcachedClient;
- import com.primeton.dgs.kernel.core.cache.ICache;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.io.Serializable;
- /**
- * @author xiongbin
- * @Version 2012-8-28
- */
- public class Memcached implements ICache<Serializable> {
- private static Logger logger = LoggerFactory.getLogger(Memcached.class);
- private MemcachedClient memcachedClient;
- public Memcached() {
- }
- public Memcached(MemcachedClient memcachedClient) {
- this.memcachedClient = memcachedClient;
- }
- @Override
- public void add(String key, Serializable value) {
- try {
- memcachedClient.add(key, 0, value);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public void replace(String key, Serializable value) {
- try {
- memcachedClient.replace(key, 0, value);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public Serializable get(String key) {
- try {
- return memcachedClient.get(key);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 移除某条缓存
- * @param key 缓存的 KEY
- * @return
- */
- @Override
- public Serializable remove(String key) {
- try {
- Serializable obj = memcachedClient.get(key);
- memcachedClient.delete(key);
- return obj;
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public int removeByPrefix(String prefix) {
- logger.warn("can not support removeByPrefix.");
- return 0;
- }
- @Override
- public void clear() {
- try {
- memcachedClient.flushAll();
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public void add(String key, int exp, Serializable value) {
- try {
- memcachedClient.add(key, exp, value);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- @Override
- public void replace(String key, int exp, Serializable value) {
- try {
- memcachedClient.replace(key, exp, value);
- } catch (Exception e) {
- throw new IllegalStateException(e);
- }
- }
- /**
- * 获得缓存队列的长度
- * @return 返回大于等于 0 则代表真实长度,返回 -1 代表无法获得
- */
- @Override public int size() {
- // memcache 无法获取当前长度
- return -1;
- }
- @Override
- public void close() throws IOException {
- memcachedClient.shutdown();
- }
- public MemcachedClient getMemcachedClient() {
- return memcachedClient;
- }
- public void setMemcachedClient(MemcachedClient memcachedClient) {
- this.memcachedClient = memcachedClient;
- }
- }
|