博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ehcache学习笔记(二) 根据条件筛选缓存中的数据
阅读量:7061 次
发布时间:2019-06-28

本文共 3083 字,大约阅读时间需要 10 分钟。

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

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
age = cache.getSearchAttribute("age"); Attribute
name = cache.getSearchAttribute("name"); //可以任务组合查询条件 query.addCriteria(name.ilike("T*").and(age.between(10, 20))); //根据查询的条件 引入结果集 query.includeValues().end(); Results results = query.execute(); List
list = results.all(); for (Result result : list) { System.out.println(result.getValue()); } }}
View Code

 

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

转载地址:http://ebyll.baihongyu.com/

你可能感兴趣的文章
React 重要的一次重构:认识异步渲染架构 Fiber
查看>>
TensorFlow笔记(2)——利用TensorFlow训练一个最简单的一元线性模型
查看>>
TensorFlow笔记(4)——优化手写数字识别模型之代价函数和拟合
查看>>
微服务java_b2b商城系统_java商城源码100%开源适合2次开发-(七)高可用的分布式配置中心(Spring Cloud Config)...
查看>>
Swift5.0新特性更新
查看>>
React Redux 中间件思想遇见 Web Worker 的灵感(附demo)
查看>>
超可爱的颜文字,我要放到代码里❛‿˂̵✧
查看>>
Laravel核心解读--观察者模式
查看>>
细数iOS上的那些安全防护
查看>>
H5打造属于自己的视频播放器(HTML篇)
查看>>
关于人工智能,你所需了解的基本知识
查看>>
2019,聊聊Web技术的发展
查看>>
centos7使用kubeadm配置高可用k8s集群的另一种方式
查看>>
深入探索 Kdump
查看>>
three.js 坐标系、camera位置属性、点、线、面
查看>>
kubernetes1.9安装dashboard,以及token认证问题
查看>>
linux tcpdump
查看>>
ASP.NET (Web) + C#算法 | 生成随机数字序列(随机数字+每个数字取随机不重复的位置和颜色)...
查看>>
理解神经网络:从神经元到RNN、CNN、深度学习
查看>>
我国第一部太赫兹视频合成孔径雷达成功研制
查看>>