NutzCN Logo
问答 ehcache属性查询问题
发布于 3109天前 作者 汉歌 2991 次浏览 复制 上一个帖子 下一个帖子
标签:

ehcache属性查询问题,如果单单往ehcache testcache中添加User对象,在查询的时候不会出问题,但是如果再往testcache添加一个对象,那就会出现查询错误

package cache;

import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = 2098760172778119467L;

	private Integer id;
	private String name;

	public User(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String toString() {
		return this.id + ":" + this.name;
	}
}
/**
 *
 * @author hange
 */
public  class CacheClass {
       public String name;
   }
 
package cache;
import java.io.Serializable;
import java.util.List;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.MemoryUnit;
import net.sf.ehcache.config.SearchAttribute;
import net.sf.ehcache.config.Searchable;
import net.sf.ehcache.search.Attribute;
import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Result;
import net.sf.ehcache.search.Results;
/**
 *
 * @author hange
 */
public class EhcacheTest {
    public static void main(String[] args){
	testEhcacheLike();
    }
    public static void testEhcacheLike(){
        net.sf.ehcache.CacheManager manager = net.sf.ehcache.CacheManager.newInstance();
        CacheConfiguration cacheConfig = new CacheConfiguration();  
        //定义属性查询
        cacheConfig.name("testCache").maxBytesLocalHeap(100, MemoryUnit.MEGABYTES);  
        //新建一个Searchable对象  
        Searchable searchable = new Searchable();  
        //给Cache配置Searchable对象,表明该Cache是一个可查询的Cache  
        cacheConfig.searchable(searchable);  
        //新建一个查询属性  
        SearchAttribute searchAttribute = new SearchAttribute();  
        //指定查询属性的名称和属性提取器的类名  
        searchAttribute.name("name"); 
        //Searchalbe对象添加查询属性  
        searchable.addSearchAttribute(searchAttribute);  
        //使用CacheConfig创建Cache对象  
        Cache cache = new Cache(cacheConfig);
        //把Cache对象纳入CacheManager的管理中  
        manager.addCache(cache);
        Cache testCache = manager.getCache("testCache");  
        //模糊查询测试
        User u = null;
        for(int i = 0; i < 100; i++){
            u = new User(i, "wanjunjun"+i);
            testCache.put(new Element(u.getId(), u));
        }
	CacheClass test cacaheObj = new CacheClass();
	cacaheObj .name = "xxx";

	testCache.put ( new Element(1001, cacaheObj ) );  //加上这个就会出现查询错误
       
        //获取名称为name的可查询属性Attribute对象  
        Attribute<String> name = testCache.getSearchAttribute("name");
        //创建一个用于查询的Query对象  
        Query query = testCache.createQuery();  
        query.includeKeys();
        query.includeValues();
        //给当前query添加一个筛选条件——可查询属性name的值等于wanjunjun
        query.addCriteria(name.ilike("wanjunjun?"));
        //执行查询操作,返回查询结果Results  
        Results results = query.execute();  
    
        manager.shutdown();  
    }

}

9 回复

报错信息贴出来

应该是没有实现序列化接口吧

```

Building nutz-plugins-daocache 1.r.55-SNAPSHOT

--- exec-maven-plugin:1.2.1:exec (default-cli) @ nutz-plugins-daocache ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" net.sf.ehcache.search.attribute.AttributeExtractorException: Bean property [name] not present on either key or value
at net.sf.ehcache.search.attribute.JavaBeanAttributeExtractor.attributeFor(JavaBeanAttributeExtractor.java:99)
at net.sf.ehcache.search.expression.ILike.execute(ILike.java:148)
at net.sf.ehcache.store.BruteForceSearchManager.executeQuery(BruteForceSearchManager.java:126)
at net.sf.ehcache.store.AbstractStore.executeQuery(AbstractStore.java:176)
at net.sf.ehcache.Cache.executeQuery(Cache.java:3919)
at net.sf.ehcache.CacheQuery.execute(CacheQuery.java:183)
at cache.EhcacheTest.testEhcacheLike(EhcacheTest.java:66)
at cache.EhcacheTest.main(EhcacheTest.java:26)

```

我发现你尝试放2种不同的对象

// 这是第一种
testCache.put(new Element(u.getId(), u));
// 第二种
testCache.put ( new Element(1001, cacaheObj ) );

日志都没配好, 加上 slf4j-log4j12.jar

@kerbores 那怎么实现序列化接口?

第二种对象没getter/setter,全是public属性? 改成getter/setter咯

搞定了,对象必须要加上 get的属性接口就可以
public class CacheClass implements Serializable {
private static final long serialVersionUID = 2098760172778119467L;
public String name;

public  Integer id;
public CacheClass ( int id, String name ){
this.id = id;
this.name = name;
}
public Integer getId() {
    return id;
}

public String getName() {
    return name;
}

public String toString() {
    return this.id + ":" + this.name;
}

}

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