【EhCache三】EhCache查询

编程技术  /  houtizong 发布于 2年前   712

本文介绍EhCache查询缓存中数据,EhCache提供了类似Hibernate的查询API,可以按照给定的条件进行查询。

 

要对EhCache进行查询,需要在ehcache.xml中设定要查询的属性

 

数据准备

    @Before    public void setUp() {        //加载EhCache配置文件        InputStream in = EhcacheTest.class.getClassLoader().getResourceAsStream("ehcache-query.xml");        CacheManager cm = CacheManager.create(in);        cache = cm.getCache("data-query");        Person p1 = new Person(1, "Tom", 34);        Person p2 = new Person(2, "Tom", 34);        Person p3 = new Person(3, "Jack", 32);        Person p4 = new Person(4, "Jack", 32);        //添加三个元素,Key是Person对象的ID        Element element = new Element(p1.getId(), p1);        cache.putIfAbsent(element);        element = new Element(p2.getId(), p2);        cache.putIfAbsent(element);        element = new Element(p3.getId(), p3);        cache.putIfAbsent(element);        //添加一个元素,Key和Value都是Person对象        cache.putIfAbsent(new Element(p4, p4));    }

 1. 在上面的测试setup方法中,为缓存添加了四个元素。前三个以person的id作为key,最后一个以person对象本身作为key 2.四个person对象,有些数据是相同的,方便后面按照Value的指定属性进行查询

 

基本查询

 对EhCache最基本的需求是对缓存元素的Key和Value进行查询,这在ehcache.xml中需要做如下配置

 

    <cache>        <searchable/>    </cache>

 

 

searchable有两个可选属性,keys和values,它们的默认值都是true,即默认情况下,EhCache已经将Key和Value的查询添加上了,查询属性分别是key和value。上面的设置等价于

 

<cache>    <searchable keys="true" values="true"/></cache>

 

 如果不允许针对keys或者values进行查询,将它置为false即可。

 

测试一:查询所有数据

 

    //查询缓存中所有的数据    @org.junit.Test    public void test0() {        Query query = cache.createQuery();        Results results = query.includeKeys().includeValues().execute();        List<Result> resultList = results.all();        Assert.assertEquals(4, resultList.size());    }

 

测试二: 根据Key(简单类型查询)进行条件查询

 

    //根据简单查询属性类型(Key是整型)进行查询    @org.junit.Test    public void test1() {        Query query = cache.createQuery();        query.includeKeys(); //查询结果中包含Key        query.includeValues(); //查询结果中包含Value        Attribute<Integer> attribute = cache.getSearchAttribute("key"); //按照EhCache内置的可查询属性key进行查询        query.addCriteria(attribute.eq(2)); //查询条件:key为2,2是简单类型        Results results = query.execute();        List<Result> resultList = results.all();        Result result = resultList.get(0);        Integer key = (Integer) result.getKey();        org.junit.Assert.assertEquals(2, key.intValue());        Person value = (Person) result.getValue();        Assert.assertEquals(value.getAge(), 34);    }

 

测试三: 根据Key(复杂类型查询)进行条件查询

样例数据中,第四个元素的key和value都是Person对象,如何创建把它查询出来?

 

@org.junit.Test    public void test2() {        Query query = cache.createQuery();        query.includeKeys();        query.includeValues();        Attribute<Person> attribute = cache.getSearchAttribute("key");        Person p4 = new Person(4, "Jack", 32);        query.addCriteria(attribute.eq(p4));        Results results = query.execute();        List<Result> resultList = results.all();        Assert.assertNotNull(resultList);        Assert.assertEquals(resultList.size() , 1);    }
 这个查询会抛出如下异常,表明,Person是一个不支持的查询类型,至于为什么会抛出这个异常,在后面讲到

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客