2 asd141747 asd141747 于 2014.07.16 20:17 提问

Mybatis 二级缓存的问题

我实现了Mybatis 提供的Cache 接口,并且实现了每个方法, 在update修改数据的时候也能调用clear,和remove 方法, 但是在delete 的时候,Mybatis 不能调用我的实现方法
,cache 接口的实现类

public class MybatisMemCached implements Cache {
private  int timeOut = 3000; // 单位毫秒

private  int cacheTime = 0; // 单位秒 0默认一个月
/** The cache service reference. */
private static  MemcachedClient memcachedClient;
{
    checkMemcachedClient();
}

private static Logger logger = LoggerFactory.getLogger(MybatisMemCached.class);

/** The ReadWriteLock. */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

private final String id;

private LinkedList<String> cacheKeys = new LinkedList<String>();

private void checkMemcachedClient() {
    if (memcachedClient == null) {
        memcachedClient = (MemcachedClient) SpringUtils.getBean("memcachedClient");
        logger.debug("memcached  server create successfully!");
    }
}

public MybatisMemCached(final  String id) {
    if (id == null) {
        throw new IllegalArgumentException("Cache instances require an ID");
    }
    this.id = id;
}

@Override
public String getId() {
    return this.id;
}

// 根据 key 从缓存中获取数据
@Override
public Object getObject(Object key) {
    String cacheKey = String.valueOf(key.hashCode());
    Object value = null;
    try {
         value = memcachedClient.get(cacheKey+"",timeOut);
    } catch (Exception e) {
        // logger.error("memcached  connection  timeout! ");
    }
    if (!cacheKeys.contains(cacheKey)) {
        cacheKeys.add(cacheKey);
    }
    return value;
}

@Override
public ReadWriteLock getReadWriteLock() {
    return this.readWriteLock;
}

// 设置数据至缓存中
@Override
public void putObject(Object key, Object value) {
    String cacheKey = String.valueOf(key.hashCode());
    if (!cacheKeys.contains(cacheKey)) {
        cacheKeys.add(cacheKey);
    }
    try {
         memcachedClient.set(cacheKey+"",cacheTime, value);
    } catch (Exception e) {
        // logger.error("memcached  connection  timeout! ");
    }
}

// 从缓存中删除指定 key 数据
@Override
public Object removeObject(Object key) {
    String cacheKey = String.valueOf(key.hashCode());
    try {
         return memcachedClient.delete(cacheKey+"");
    } catch (Exception e) {
         logger.error("memcached  connection  timeout! ");
    }
    cacheKeys.remove(cacheKey);
    return null;
}

// 清空当前 Cache 实例中的所有缓存数据
@Override
public void clear() {
    for (int i = 0; i < cacheKeys.size(); i++) {
        String cacheKey = cacheKeys.get(i);
        try {
             memcachedClient.delete(cacheKey+"");
        } catch (Exception e) {
             logger.error("memcached  connection  timeout! ");
        }
    }
    cacheKeys.clear();
}

@Override
public int getSize() {
    return cacheKeys.size();
}


@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof Cache)) {
        return false;
    }
    Cache otherCache = (Cache) obj;
    return this.id.equals(otherCache.getId());
}

@Override
public int hashCode() {
    return this.id.hashCode();
}

@Override
public String toString() {
    return "OSCache {"
            + this.id
            + "}";
}

public void setTimeOut(int timeOut) {
    this.timeOut = timeOut;
}

public void setCacheTime(int cacheTime) {
    this.cacheTime = cacheTime;
}

}

xml文件映射 我也加了

除了delete 标签不能清除缓存,其他的都能自动清除缓存 我也加过flushCache="true" 这个属性,但是不好使,请大神帮忙

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!