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

相关文章:

  • 2021-05-29
  • 2022-12-23
  • 2021-07-20
  • 2021-12-16
  • 2021-05-16
  • 2022-12-23
  • 2022-03-01
猜你喜欢
  • 2022-12-23
  • 2023-01-20
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
相关资源
相似解决方案