NutzCN Logo
分享 Ehcache的封装成个Map接口? 类似的
发布于 3116天前 作者 wendal 2106 次浏览 复制 上一个帖子 下一个帖子
标签:

概念实现

package net.wendal.nutzbook.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

public class SuperCache<K,V> {

    protected Cache cache;
    
    public SuperCache(Cache cache) {
        this.cache = cache;
    }
    
    public void put(K k, V v) {
        cache.put(new Element(k, v));
    }
    
    @SuppressWarnings("unchecked")
    public V get(K k) {
        Element ele = cache.get(k);
        if (ele == null)
            return null;
        return (V)ele.getObjectValue();
    }
}

1 回复

加个查询

package net.wendal.nutzbook.util;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;

@SuppressWarnings("unchecked")
public class SuperCache<K,V> {

    protected Cache cache;
    
    public SuperCache(Cache cache) {
        this.cache = cache;
    }
    
    public void put(K k, V v) {
        cache.put(new Element(k, v));
    }
    
    public V get(K k) {
        Element ele = cache.get(k);
        if (ele == null)
            return null;
        return (V)ele.getObjectValue();
    }
    
    public List<V> query(Query query) {
        Results results = query.execute();
        List<Result> resultList = results.all();
        List<V> list = new ArrayList<>();
        for(Result result : resultList){
            list.add((V)result.getValue());
        }
        return list;
    }
    
    public Cache getCache() {
        return cache;
    }
    
    public Query makeQuery() {
        return cache.createQuery();
    }
}

添加回复
请先登陆
回到顶部