asd141747 2014-07-16 12:17 采纳率: 0%
浏览 4895

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" 这个属性,但是不好使,请大神帮忙

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:33
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?