Ehcache学习笔记(二) 根据条件筛选缓存中的数据

Ehcache学习笔记(二)  根据条件筛选缓存中的数据

 

Ehcache提供了很方便的索引机制,有的时候我们需要根据一些其他的条件对缓存中的数据进行索引,而不是简单根据KEY来进行索引。

 

这是实体类 没什么好说的

package com.epkj.test;



import java.util.Date;



public class User implements java.io.Serializable {



    private int id;

    

    private String name;

    

    private int age;

    

    public int getAge() {

        return age;

    }



    public void setAge(int age) {

        this.age = age;

    }



    private Date brithray;

    

    private double money;



    public User() {

        super();

    }



    public User(int id, String name, int age, Date brithray, double money) {

        super();

        this.id = id;

        this.name = name;

        this.age = age;

        this.brithray = brithray;

        this.money = money;

    }



    @Override

    public String toString() {

        return this.id + "====" + this.age;

    }

    

    public int getId() {

        return id;

    }



    public void setId(int id) {

        this.id = id;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public Date getBrithray() {

        return brithray;

    }



    public void setBrithray(Date brithray) {

        this.brithray = brithray;

    }



    public double getMoney() {

        return money;

    }



    public void setMoney(double money) {

        this.money = money;

    }

}

 

 主要看XML

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

         updateCheck="true" monitoring="autodetect"

         dynamicConfig="true">

         

    <diskStore path="F:/"/>

    

    <cache 

        name="sampleCache" 

        maxElementsInMemory="1000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="120"

        memoryStoreEvictionPolicy="LRU"

    >

        <searchable>

            <searchAttribute name="name"/>

            <searchAttribute name="age" expression="value.getAge()"/>

        </searchable>

    </cache>

    

    <!-- 

        //定义查询的属性 可以有多重方式,这里不一一举例

        <searchable>

            <searchAttribute name="name"/>

            <searchAttribute name="age" expression="value.getAge()"/>

        </searchable>

        

        <searchable>

            <searchAttribute name="age" expression="value.person.getAge()"/>

        </searchable>

        

        <searchable>

             <searchAttribute name="name" expression="element.toString()"/>

          </searchable>

     -->

</ehcache>
View Code

 

 查询的代码

package com.epkj.test;



import java.util.Date;

import java.util.List;



import net.sf.ehcache.CacheManager;

import net.sf.ehcache.Ehcache;

import net.sf.ehcache.Element;

import net.sf.ehcache.search.Attribute;

import net.sf.ehcache.search.Query;

import net.sf.ehcache.search.Result;

import net.sf.ehcache.search.Results;



public class SerachTest {



    /**

     * @param args

     */

    public static void main(String[] args) {

        

        CacheManager manager = CacheManager.getInstance();

        

        Ehcache cache = manager.getCache("sampleCache");

        //创建测试数据放入缓存

        for (int i = 0; i < 50; i++) {

            Element element = null;

            if(i <= 20) {

                element = new Element(i, new User(1, "Txxxxxx" + i, i, new Date(), 3500));

            } else {

                element = new Element(i, new User(1, "Yxxxxxx" + i, i, new Date(), 3500));

            }

            cache.put(element);

        }

        

        //创建Query接口 用法跟hibernate的Query类似

        Query query = cache.createQuery();

        //取得属性

        Attribute<Integer> age = cache.getSearchAttribute("age");

        

        Attribute<String> name = cache.getSearchAttribute("name");

        

        //可以任务组合查询条件

        query.addCriteria(name.ilike("T*").and(age.between(10, 20)));

        

        //根据查询的条件 引入结果集

        query.includeValues().end();

        

        Results results = query.execute();

        

        List<Result> list = results.all();

        

        for (Result result : list) {

            System.out.println(result.getValue());

        }

    }



}
View Code

 

以上是对Ehcache缓存元素的条件查询的一个总结。

你可能感兴趣的:(ehcache)