Ehcache(05)——緩存的查詢


 

https://www.iteye.com/blog/elim-2117505

緩存的查詢

目錄

1.    使Cache可查詢

1.1     基於Xml配置

1.2     基於代碼的配置

2     指定可搜索的屬性

2.1     可查詢屬性類型

2.2     屬性的提取

2.2.1    定義自己的AttributeExtractor

2.2.2    JavaBeanAttributeExtractor

2.2.3    ReflectionAttributeExtractor

2.2.4    DynamicAttributesExtractor

2.3     通過程序指定可查詢屬性

3     查詢

3.1     創建查詢與篩選條件

3.1.1    獲取查詢屬性

3.1.2    篩選類型

3.2     查詢內容

3.3     結果

3.4     統計

3.5     排序

3.6     分組

3.7     讓Query不可變

3.8     對BeanShell的支持

3.9     小結

 

       Ehcache中為我們提供了可以對Cache中緩存的元素進行查找的方式。其邏輯類似於SQL中的查找。通過給定各種限制條件,我們可以構造各種復雜的查詢,然后返回結果集,也可以對查詢進行分組和排序等。

1.   使Cache可查詢

       Ehcache中的查詢是針對於Cache而言的。但並不是所有的Cache都可以進行查詢操作,我們需要指定其為一個可查詢的Cache之后才可以對該Cache進行查詢操作。因為在配置Cache的時候有基於xml文件的配置和基於程序代碼的配置,所以對應的使一個Cache可查詢也有兩種方式。

1.1     基於Xml配置

       當我們的Cache定義是基於Xml文件的配置時,我們只需在對應Cache定義下聲明一個子元素searchable即可使當前Cache擁有可查詢的功能。

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable/>  
  3. </cache>  

 

 

1.2     基於代碼的配置

       基於代碼的配置是通過新建Searchable對象,然后指定需要設置為可查詢Cache對應的CacheConfiguration的Searchable對象為我們新建的Searchable對象即可。

Java代碼  
  1.    public void test() {  
  2.       CacheManager cacheManager = CacheManager.create();  
  3.       CacheConfiguration cacheConfig = new CacheConfiguration();  
  4.       cacheConfig.name("cache1").maxBytesLocalHeap(100, MemoryUnit.MEGABYTES);  
  5.       Searchable searchable = new Searchable();  
  6.       //指定Cache的Searchable對象。  
  7.       cacheConfig.searchable(searchable);  
  8.       //如下指定也行  
  9. //    cacheConfig.addSearchable(searchable);  
  10.       Cache cache1 = new Cache(cacheConfig);  
  11.       cacheManager.addCache(cache1);  
  12.    }  

 

 

2       指定可搜索的屬性

       配置了Cache可查詢后,我們還需要配置當前Cache可以對哪些屬性進行查詢,即可以把哪些屬性作為條件來對Cache進行查詢。在Ehcache中使用一個net.sf.ehcache.search.Attribute來表示一個可查詢的屬性。這些可查詢的屬性可以是我們的key、value或者它們對應的屬性。定義可查詢屬性是通過searchable元素的子元素searchAttribute來定義的,如:

Xml代碼  
  1. <cache name="userCache" maxBytesLocalHeap="50M">  
  2.    <searchable>  
  3.       <searchAttribute name="name"/>  
  4.    </searchable>  
  5. </cache>  

 

       其中name表示我們所定義的可查詢屬性的名稱,是必須指定的屬性。這里會通過屬性提取機制提取key或者value中name所對應的屬性,這里是name屬性,來進行索引。關於屬性提取機制將在后續講解。

2.1     可查詢屬性類型

       並不是所有的屬性都可以用來作為Cache的可查詢屬性,它必須是以下類型之一:

l  Boolean

l  Byte

l  Short

l  Character

l  Integer

l  Long

l  Float

l  Double

l  String

l  java.util.Date

l  java.sql.Date

l  Enum

 

       默認情況下,系統會自動把我們存入可查詢Cache中元素的key和value作為可查詢屬性,命名為key和value,當它們是以上可查詢類型時我們可以直接對它們進行查詢。如果不需要默認將我們的key和value作為可查詢屬性的話,我們可以在指定Cache為一個可查詢Cache時指定searchable元素的keys屬性和values屬性為false即可。如:

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable keys="false" values="false"/>  
  3. </cache>  

 

 

2.2     屬性的提取

       當我們的key或者value不是可查詢類型,然而我們又希望對它們進行查詢時,我們就需要把key或者value中的屬性提取出來作為Cache的一個可查詢屬性。這是通過AttributeExtractor來進行的,AttributeExtractor是一個接口,其中只定義了一個方法Object attributeFor(Element element, String attributeName)。其返回值必須是可查詢屬性類型之一。當然,返回null也是可以的。下面我們來看看如何定義自己的AttributeExtractor。

2.2.1   定義自己的AttributeExtractor

       假設我們有一個名叫userCache的緩存,其中存放的元素值都是一個User對象。而我們的User對象有一個String類型的name屬性。假設我們現在指定了我們的userCache的一個可查詢屬性為user,而其真正對應的內容是我們的Element中存放的value的name。(這個需求可能會比較奇怪)。那么這個時候我們的AttributeExtractor實現大概會是這個樣子:

Java代碼  
  1. public class UserAttributeExtractor implements AttributeExtractor {  
  2.    
  3.    @Override  
  4.    public Object attributeFor(Element element, String attributeName)  
  5.          throws AttributeExtractorException {  
  6.       User user = (User) element.getObjectValue();  
  7.       return user.getName();  
  8.    }  
  9.    
  10. }  
  11.    

 

       定義好了AttributeExtractor之后,我們要告訴Ehcache,緩存userCache的可查詢屬性user對應的AttributeExtractor是我們定義的UserAttributeExtractor,這只需要指定searchAttribute元素的class屬性即可。

Xml代碼  
  1. <cache name="userCache" maxBytesLocalHeap="50M">  
  2.    <searchable>  
  3.       <searchAttribute name="user" class="com.xxx.UserAttributeExtractor"/>  
  4.    </searchable>  
  5. </cache>  

 

       之后我們通過user屬性來查詢時就可以通過User對象的name屬性來過濾一些結果集了。如果我們的AttributeExtractor還需要接收其它的參數的話,我們可以通過searchAttribute元素的properties屬性來指定,其對應的參數是鍵值對的形式,中間用等號“=”隔開,多個參數之間用逗號隔開。如:

Xml代碼  
  1. <cache name="userCache" maxBytesLocalHeap="50M">  
  2.    <searchable>  
  3.       <searchAttribute name="user" class="com.xxx.UserAttributeExtractor" properties="a=1,b=2"/>  
  4.    </searchable>  
  5. </cache>  

 

       我們指定了properties屬性后,我們對應的AttributeExtractor必須給定一個以Properties對象為參數的構造方法才可以接收到這些指定的參數。

 

       除了定義自己的屬性提取實現類之外,Ehcache還為我們提供了一些實現類。包括KeyObjectAttributeExtractor、ValueObjectAttributeExtractor,這兩個屬性提取器就是默認情況下Ehcache用來把key和value提取為一個可查詢屬性的方式。此外還有JavaBeanAttributeExtractor和ReflectionAttributeExtractor。

2.2.2   JavaBeanAttributeExtractor

       當我們定義一個可查詢屬性searchAttribute只指定了其name屬性時,系統所使用的AttributeExtractor就是JavaBeanAttributeExtractor。該AttributeExtractor會從元素的key或者value中取searchAttribute的name屬性值所對應的屬性。如果我們有如下這樣一個可查詢緩存的定義,我們的Ehcache在給可查詢屬性address建立索引時就會獲取元素key的address屬性或者value的address屬性來作為查詢屬性address的值。

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable keys="false" values="false">  
  3.       <searchAttribute name="address"/>  
  4.    </searchable>  
  5. </cache>  

 

 

       注意:使用JavaBeanAttributeExtractor時,如果key和value中都包含可查詢屬性,則系統會拋出異常,如果都不包含的話也會拋出異常。

 

2.2.3   ReflectionAttributeExtractor

       當我們定義一個可查詢屬性searchAttribute時指定了expression屬性時,系統就會使用ReflectionAttributeExtractor來提取屬性的值。此屬性提取器是通過反射來提取屬性值的。expression必須以key、value或element開始,然后中間以點“.”來連接它們所對應的屬性或方法,以及屬性的屬性,方法的方法。key表示元素的key,value表示元素的value,element表示元素本身。下面來看幾個示例。

       1.查詢屬性address的值是對應的value的address屬性。

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable keys="false" values="false">  
  3.       <searchAttribute name="address" expression="value.address"/>  
  4.    </searchable>  
  5. </cache>  

 

 

       2.查詢屬性address的值是對應的value的extraInfo屬性的getAddress()方法的返回值。

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable keys="false" values="false">  
  3.       <searchAttribute name="address" expression="value.extraInfo.getAddress()"/>  
  4.    </searchable>  
  5. </cache>  

 

 

       3.查詢屬性hitCount的值是對應的element的getHitCount()方法的返回值。

Xml代碼  
  1. <cache name="searchableCache" maxBytesLocalHeap="100M">  
  2.    <searchable keys="false" values="false">  
  3.       <searchAttribute name="hitCount" expression="element.getHitCount()"/>  
  4.    </searchable>  
  5. </cache>  

 

 

2.2.4   DynamicAttributesExtractor

       之前介紹的AttributeExtractor都是在Cache實例化之前定義的,其會在Cache實例化時初始化這些可查詢屬性。而DynamicAttributesExtractor允許我們在Cache實例化后添加可查詢屬性。DynamicAttributesExtractor是一個接口,它跟AttributeExtractor接口沒有任何關系。該接口中僅定義了一個方法attributesFor(),該方法將接收一個Element對象作為參數,然后返回一個將作為可查詢屬性的Map,該Map的key對應可查詢屬性的名稱,而value則對應可查詢屬性的值。那么我們在實現DynamicAttributesExtractor接口時只需要實現attributesFor()方法即可。

       使用DynamicAttributeExtractor時我們的Cache對應的Searchable必須是支持該提取器才行,這是通過Searchable對象的allowDynamicIndexing屬性來指定的,使用xml配置時該屬性是直接配置在searchable元素上的,而使用程序來定義時則需要通過Searchable對象來指定了。之后我們需要把它注冊給我們的Cache。通過Cache的registerDynamicAttributesExtractor()方法我們就可以給Cache注冊一個動態的屬性提取器了,該提取器將在往Cache中put或者replace元素時被調用。通過文字說明會比較抽象,接下來我們來看一個相應的示例。

       假設我們定義了如下這樣一個專門用來緩存User的Cache,其中User中含有屬性name。我們在定義該Cache的時候即指定了其是一個可查詢的Cache,同時通過指定allowDynamicIndexing為true使其支持動態屬性提取,我們還給該Cache指定了一個可查詢屬性name。

Xml代碼  
  1. <cache name="userCache" maxBytesLocalHeap="50M">  
  2.    <searchable allowDynamicIndexing="true">  
  3.       <searchAttribute name="name" expression="value.getName()"/>  
  4.    </searchable>  
  5. </cache>  

 

       接下來我們將在該Cache初始化之后注冊一個DynamicAttributesExtractor,用於索引元素被查詢到的次數hitCount。代碼如下所示,我們在userCache初始化后給其注冊了一個DynamicAttributesExtractor,在DynamicAttributesExtractor實現類中我們實現了attributesFor方法,在該方法體內我們構造了一個Map,並往其中放入了一個key為hitCount的元素。當我們往userCache中put或者replace元素的時候,就會觸發我們注冊的DynamicAttributesExtractor的attributesFor方法,然后Ehcache會對返回的動態可查詢屬性hitCount進行索引。在下面的代碼中,我們的在給userCache注冊了DynamicAttributesExtractor之后,馬上列出其中包含的可查詢屬性,這個時候肯定只會包含預定義好的key、value和name,因為我們注冊的DynamicAttributesExtractor還沒有被執行。之后往其中放入元素之后,userCache中包含的可查詢屬性才會有通過DynamicAttributesExtractor返回的hitCount。

Java代碼  
  1. @Test  
  2. public void dynamicExtractor() {  
  3.    CacheManager cacheManager = CacheManager.create();  
  4.    Cache userCache = cacheManager.getCache("userCache");  
  5.    userCache.registerDynamicAttributesExtractor(new DynamicAttributesExtractor() {  
  6.   
  7.       @Override  
  8.       public Map<String, Object> attributesFor(Element element) {  
  9.          Map<String, Object> attrMap = new HashMap<String, Object>();  
  10.          attrMap.put("hitCount", element.getHitCount());  
  11.          return attrMap;  
  12.       }  
  13.        
  14.    });  
  15.    this.listSearchableAttrs(userCache); //key、value和name  
  16.    userCache.put(new Element("1", new User()));  
  17.    this.listSearchableAttrs(userCache); //key、value、name和hitCount  
  18. }  
  19.   
  20. /** 
  21.  * 輸出當前Ehcache中可查詢的屬性 
  22.  * @param cache 
  23.  */  
  24. private void listSearchableAttrs(Ehcache cache) {  
  25.    Set<Attribute> attrSet = cache.getSearchAttributes();  
  26.    for (Attribute attr : attrSet) {  
  27.       System.out.println(attr.getAttributeName());  
  28.    }  
  29. }  

 

       一個Cache只能注冊有一個DynamicAttributesExtractor,當同時注冊多個時,后者會將前者覆蓋。但是DynamicAttributesExtractor和其它AttributeExtractor是可以並存的,所以因為其它AttributeExtractor是在Cache初始化前定義的,所以DynamicAttributesExtractor不能返回已經通過AttributeExtractor提取過的同名屬性。

 

2.3     通過程序指定可查詢屬性

       通過前面的內容我們知道設置可查詢屬性時除了DynamicAttributesExtractor可以在Cache初始化后再添加可查詢屬性外,我們的可查詢屬性必須是在Cache初始化之前進行指定,否則在對Cache進行查詢時我們就不能使用該查詢屬性進行查詢。如下面這一段代碼,我們在Cache初始化后通過獲取其配置信息,再往其對應的Searchalbe對象中新增一個名叫hello的查詢屬性,那么我們在今后對該Cache進行查詢時將不能使用hello屬性進行查詢。

Java代碼  
  1. @Test  
  2. public void setSearchAttrInProgram() {  
  3.    CacheManager cacheManager = CacheManager.create();  
  4.    Cache cache = cacheManager.getCache("searchableCache");  
  5.    CacheConfiguration cacheConfig = cache.getCacheConfiguration();  
  6.    Searchable searchable = cacheConfig.getSearchable();  
  7.    SearchAttribute searchAttribute = new SearchAttribute();  
  8.    searchAttribute.name("hello");  
  9.    searchable.addSearchAttribute(searchAttribute);  
  10.    this.listSearchableAttrs(cache);  
  11. }  

 

       由於定義非動態查詢屬性時需要在Cache初始化時定義,所以當我們需要在程序中定義查詢屬性時對應的Cache也需要是在程序中聲明的才行。下面是在程序中指定可查詢屬性的一個示例。

Java代碼  
  1. @Test  
  2. public void setSearchAttrInProgram() {  
  3.    CacheManager cacheManager = CacheManager.create();  
  4.    CacheConfiguration cacheConfig = new CacheConfiguration();  
  5.    cacheConfig.name("cacheName").maxBytesLocalHeap(100, MemoryUnit.MEGABYTES);  
  6.    //新建一個Searchable對象  
  7.    Searchable searchable = new Searchable();  
  8.    //給Cache配置Searchable對象,表明該Cache是一個可查詢的Cache  
  9.    cacheConfig.searchable(searchable);  
  10.    //新建一個查詢屬性  
  11.    SearchAttribute searchAttribute = new SearchAttribute();  
  12.    //指定查詢屬性的名稱和屬性提取器的類名  
  13.    searchAttribute.name("查詢屬性名稱");  
  14.    //searchAttribute.className("屬性提取器的類名");  
  15.    //Searchalbe對象添加查詢屬性  
  16.    searchable.addSearchAttribute(searchAttribute);  
  17.    //使用CacheConfig創建Cache對象  
  18.    Cache cache = new Cache(cacheConfig);  
  19.    //把Cache對象納入CacheManager的管理中  
  20.    cacheManager.addCache(cache);  
  21.    this.listSearchableAttrs(cache);  
  22. }  

 

3       查詢

       在Ehcache中是通過一個net.sf.ehcache.search.Query對象來表示一個查詢的,通過該對象我們可以對緩存中的元素進行查詢,查詢條件就是我們之前定義好的可查詢屬性,而查詢結果可以是緩存的key、value或可查詢屬性,也可以是針對於可查詢屬性的一些統計結果。

3.1     創建查詢與篩選條件

       在對Cache進行查詢前我們需要先創建一個Query對象。Query對象是通過EhCache接口定義的createQuery()方法創建的,Cache類對它進行了實現。有了Query對象之后,我們需要使用Query對象的addCriteria(Criteria criteria)方法給該Query對象添加一些限制條件來對其中緩存的元素進行篩選,否則返回的結果將是針對於所有的緩存元素的。

Java代碼  
  1. @Test  
  2. public void search () {  
  3.    CacheManager cacheManager = CacheManager.create();  
  4.    Cache userCache = cacheManager.getCache("userCache");  
  5.    User user;  
  6.    for (int i=0; i<10; i++) {  
  7.       user = new User(i, "name"+(i%2), 30+i);  
  8.       userCache.put(new Element(user.getId(), user));  
  9.    }  
  10.    Query query = userCache.createQuery();  
  11. }  

 

       Criteria是一個接口,在net.sf.ehcache.search.expression定義了其一系列的實現類,我們也可以直接通過new一個Criteria實現類的實例來對Query結果進行篩選。但通常我們不需要這樣做,因為Ehcache中已經為我們實現了的Criteria通常已經可以滿足我們的需求了。Ehcache中代表查詢屬性的Attribute類已經為我們提供了獲取針對於該屬性的各種Criteria的方法。好,現在我們已經知道了可以通過查詢屬性直接獲取到針對於該屬性的限制Criteria對象,那么我們該如何獲取查詢屬性呢?

3.1.1   獲取查詢屬性

       獲取查詢屬性Attribute主要有兩種方式,一種是直接new一個Attribute實例對象,另一種是通過Ehcache接口定義的getSearchAttribute(String attrName)獲取到可查詢緩存中對應屬性名稱的可查詢屬性對象Attribute。常用的還是通過getSearchAttribute(String attrName)方法來獲取對應的查詢屬性Attribute。當調用可查詢Cache的getSearchAttribute(String attrName)方法來獲取當前緩存的可查詢屬性時,如果對應名稱的可查詢屬性不存在,則會拋出異常。

Java代碼  
  1. CacheManager cacheManager = CacheManager.create();  
  2. Cache cache = cacheManager.getCache("userCache");  
  3. Attribute<String> name = cache.getSearchAttribute("name");  

 

       Attribute類使用了泛型定義,其表示當前屬性值的類型。

3.1.2   篩選類型

       有了可查詢屬性Attribute之后,我們就可以通過Attribute類定義的一系列方法獲取到當前Attribute的某種限制,從而對Query的查詢結果進行篩選。如我們要篩選name為“name1”的查詢結果時我們可以通過name.eq(“name1”)來進行篩選。

Java代碼  
  1. public void search2() {  
  2.    CacheManager cacheManager = CacheManager.create();  
  3.    Cache userCache = cacheManager.getCache("userCache");  
  4.    User user;  
  5.    for (int i=0; i<10; i++) {  
  6.       user = new User(i, "name"+(i%2), 30+i);  
  7.       userCache.put(new Element(user.getId(), user));  
  8.    }  
  9.    //獲取名稱為name的可查詢屬性Attribute對象  
  10.    Attribute<String> name = userCache.getSearchAttribute("name");  
  11.    //創建一個用於查詢的Query對象  
  12.    Query query = userCache.createQuery();  
  13.    //給當前query添加一個篩選條件——可查詢屬性name的值等於“name1”  
  14.    query.addCriteria(name.eq("name1"));  
  15. }  

 

       接下來我們來看一下Attribute類為我們提供的獲取對應Criteria的方法有哪些。

 

Attribute方法

對應Criteria實現類

描述

between

Between

屬性值在給定的范圍之間

in

InCollection

在給定的集合之中

ne

NotEqualTo

不等於給定的值

eq

EqualTo

等於給定的值

lt

LessThan

小於給定的值

le

LessThanOrEqual

小於或等於給定的值

gt

GreaterThan

大於給定的值

ge

GreaterThanOrEqual

大於或等於給定的值

ilike

ILike

匹配給定的表達式,表達式中可以使用“*”來代表任意多個字符,使用“?”來代表任意一個字符

notIlike

NotILike

不匹配給定的表達式

isNull

IsNull

等於null

notNull

NotNull

不等於null

 

       那當我們要實現與或非的邏輯時怎么辦呢?Criteria為我們提供了對應的方法,分別對應and(Criteria criteria)方法、or(Criteria criteria)方法和not()方法,然后這三個方法的返回結果還是一個Criteria,它們對應的Criteria實現類分別為And、Or和Not。當我們使用Query的addCriteria(Criteria criteria)方法來添加一個篩選條件時默認都是對應的and操作。

       下面我們來看一些使用Criteria的例子。先假設我們有如下定義的一個Cache,其中存放的元素的value都是一個User對象,下面將給出一些針對於該Cache使用Criteria進行篩選查詢的一些示例。

Xml代碼  
  1. <cache name="userCache" maxBytesLocalHeap="50M">  
  2.    <searchable>  
  3.       <searchAttribute name="name" expression="value.getName()"/>  
  4.       <searchAttribute name="age"/>  
  5.       <searchAttribute name="unitNo" expression="value.unit.unitNo"/>  
  6.       <searchAttribute name="unitName" expression="value.unit.getUnitName()"/>  
  7.       <searchAttribute name="mobile" expression="value.getMobile()"/>  
  8.       <searchAttribute name="hitCount" expression="element.getHitCount()"/>  
  9.    </searchable>  
  10. </cache>  

 

1、年齡在25歲到35歲之間且屬於單位002的。

Java代碼  
  1.    Attribute<Integer> age = userCache.getSearchAttribute("age");  
  2.    Attribute<String> unitNo = userCache.getSearchAttribute("unitNo");  
  3.    query.addCriteria(age.between(25, 35).and(unitNo.eq("002")));  
  4.    //或者使用兩次addCriteria  
  5. // query.addCriteria(age.between(25, 35)).addCriteria(unitNo.eq("002"));  

 

 

2、屬於單位002或者單位003,手機號碼以137開始且年齡大於35歲的。

Java代碼  
  1. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  2. Attribute<String> unitNo = userCache.getSearchAttribute("unitNo");  
  3. Attribute<String> mobile = userCache.getSearchAttribute("mobile");  
  4. ry.addCriteria(age.gt(35).and(unitNo.eq("002").or(unitNo.eq("003"))).and(mobile.ilike("137*")));  

 

 

3、不屬於單位002且年齡小於30的。

Java代碼  
  1. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  2. Attribute<String> unitNo = userCache.getSearchAttribute("unitNo");  
  3. query.addCriteria(unitNo.ne("002").and(age.lt(30)));  
  4. //或者使用not()方法  
  5. query.addCriteria(unitNo.eq("002").not().and(age.lt(30)));  

 

 

3.2     查詢內容

       一個Query在查詢之前,我們必須告訴它需要查詢什么內容,也就是說查詢的結果中會包含哪些信息。如果在執行查詢操作之前沒有告訴Query我們要查詢什么內容,Ehcache將拋出異常。可以查詢的內容包括緩存中存入元素的key、value,可查詢屬性對應的值,以及針對於當前查詢結果中某個可查詢屬性的統計信息。針對於這四種可以查詢內容Query中提供了四個include方法來表示當前Query的查詢結果中會包含對應的內容。下面用一個表格來做個展示。

Query方法

描述

includeKeys()

查詢結果中包含所存元素的key

includeValues()

查詢結果中包含所存元素的value

includeAttribute(Attribute<?>... attributes)

查詢結果中要包含的可查詢屬性

includeAggregator(Aggregator... aggregators)

查詢結果中所要包含的統計信息,關於Aggregator將在后文介紹統計的時候進行講解

 

       如下的代碼表示我們的查詢結果中會包含元素的key、可查詢屬性name和age對應的值。

Java代碼  
  1. Attribute<String> name = userCache.getSearchAttribute("name");  
  2. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  3. query.includeAttribute(name, age);  

 

       在實際應用中,為了讓我們的程序具有更好的性能,我們的查詢結果最好只包含我們需要的信息。如只需要獲取某個屬性的值就不必返回整個value。

3.3     結果

       有了Query之后我們就可以來執行對應的查詢操作,獲取返回的查詢結果。通過調用Query的execute()方法就可以對當前Query執行查詢操作,並獲取其返回的結果。Ehcache中使用一個Results接口來代表一個Query的查詢結果,使用Result接口來代表對應的一條記錄。Results中定義了一個方法all()用於返回查詢出來的所有Result組成的List,查詢的緩存中有多少元素滿足查詢條件,查詢結果Results中就會包含多少個Result對象。Result中定義有getKey()、getValue()、getAttribute()和getAggregatorResults()方法用於獲取查詢結果中對應元素的key、value、可查詢屬性對應的值,以及針對於當前查詢的統計信息組成的List。如果查詢結果中不包含對應的信息,那么在Result調用對應方法獲取信息時將拋出異常。Results針對於查詢結果中是否包含這四方面的信息給我們提供了四個has方法:hasKeys()、hasValues()、hasAttributes()和hasAggregators()。Results和Result這兩個接口Ehcache中都已經存在對應的實現了,我們在使用時只要直接利用接口來進行操作就可以了。

Java代碼  
  1. //執行查詢操作,返回查詢結果Results  
  2. Results results = query.execute();  
  3. //獲取Results中包含的所有的Result對象  
  4. List<Result> resultList = results.all();  
  5. if (resultList != null && !resultList.isEmpty()) {  
  6.    for (Result result : resultList) {  
  7.       //結果中包含key時可以獲取key  
  8.       if (results.hasKeys()) {  
  9.          result.getKey();  
  10.       }  
  11.       //結果中包含value時可以獲取value  
  12.       if (results.hasValues()) {  
  13.          result.getValue();  
  14.       }  
  15.       //結果中包含屬性時可以獲取某個屬性的值  
  16.       if (results.hasAttributes()) {  
  17.          Attribute<String> attribute = userCache.getSearchAttribute("name");  
  18.          result.getAttribute(attribute);  
  19.       }  
  20.       //結果中包含統計信息時可以獲取統計信息組成的List  
  21.       if (results.hasAggregators()) {  
  22.          result.getAggregatorResults();  
  23.       }  
  24.    }  
  25. }  

 

       當然,如果你已經清楚的知道了查詢結果中已經包含了key時你在獲取key前就可以不用調用Results的hasKeys()方法進行判斷了,其它結果也一樣。

       Results中的all()方法可以返回當前查詢的結果中的所有Result組成的List。另外,Results中還提供了一個range(int start, int count)方法用於獲取當前結果集的一個子集,其底層默認實現使用的是List的subList()方法。該方法可以用於對查詢結果的分頁操作。

       默認情況下,我們在對Cache進行查詢時,查詢結果將返回所有滿足查詢條件的記錄。當返回的記錄非常多時,系統可能會因為內存不足而報錯。Query中定義了一個maxResults(int maxResults)方法用於限制當前查詢返回查詢結果的最大記錄數。

       需要注意的是由於元素過期的問題,我們查詢結果中的元素不一定還存在。

       當我們利用完Results之后,我們需要通過調用Results的discard()方法來釋放資源。

 

3.4     統計

       Ehcache為我們提供了一個Aggregator接口用於在查詢過程中對某個查詢屬性進行統計。我們可以實現自己的Aggregator,也可以使用Ehcache為我們提供的實現類。Ehcache中已經為我們提供了五個Aggregator實現類,分別是Min、Max、Sum、Count和Average。看了名稱我應該就知道這五個Aggregator分別是做什么用的。Min是求最小值、Max是求最大值、Sum是求和、Count是計數、Average是求平均值。那么在使用這五個Aggregator時也是非常方便的,因為我們的Attribute已經為我們針對這五個Aggregator定義了對應的方法。方法名稱就是對應Aggregator實現類簡稱的首字母小寫,如Min在Attribute中就對應min()方法。

       當我們需要對某個查詢屬性進行統計時,我們需要把對應的Aggregator通過調用Query的includeAggregator()方法添加到查詢的結果中。

Java代碼  
  1. //創建一個用於查詢的Query對象  
  2. Query query = userCache.createQuery();  
  3. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  4. //查詢結果中包含age的平均值和age的最大值  
  5. query.includeAggregator(age.average(), age.max());  
  6. Results results = query.execute();  
  7. List<Result> resultList = results.all();  
  8. if (resultList != null && !resultList.isEmpty()) {  
  9.    //每一個查詢結果Result中都會包含對查詢結果的統計信息。  
  10.    Result result = resultList.get(0);  
  11.    //多個統計信息將會組成一個List進行返回  
  12.    List<Object> aggregatorResults = result.getAggregatorResults();  
  13.    Number averageAge = (Number)aggregatorResults.get(0);  
  14.    Integer maxAge = (Integer)aggregatorResults.get(1);  
  15.    System.out.println(averageAge + "---" + maxAge);  
  16. }  

 

       當我們的查詢結果中只包含有統計信息時,我們的查詢結果Results中只會有一條記錄,即一個Result對象。當包含其它信息時查詢結果就可能會有多條記錄,而且每條記錄中都會包含有對應的統計信息。

 

3.5     排序

       Ehcache中對於Cache的查詢也是可以進行排序的,這是通過Query的addOrderBy()方法來指定的。該方法接收兩個參數,第一個參數表示需要進行排序的屬性Attribute,第二個參數是排序的方向Direction。Direction有兩個可選值,Direction.ASCENDING和Direction.DESCENDING。當需要對多個屬性進行排序時則需要調用多次addOrderBy()方法。

Java代碼  
  1. Attribute<String> unitNo = userCache.getSearchAttribute("unitNo");  
  2. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  3. //查詢結果按部門編號的升序和年齡的降序進行排列  
  4. query.addOrderBy(unitNo, Direction.ASCENDING).addOrderBy(age, Direction.DESCENDING);  

 

 

3.6     分組

       Ehcache也支持對查詢的緩存進行分組。這是通過Query的addGroupBy()方法來定義的,該方法接收一個Attribute作為參數,表示要對哪個Attribute進行分組,當需要對多個Attribute進行分組時,則需要調用多次addGroupBy()方法。使用分組的語法基本上跟SQL里面分組的語法是一樣的,當使用分組時查詢結果只能包含分組的屬性和統計信息,統計信息是對分組后的情況進行統計。唯一不同的是Ehcahce中查詢分組時無法對分組后的情況進行篩選。

       以下是一個通過單位編碼進行分組統計各單位員工的平均年齡、最大年齡和員工的人數的示例。

Java代碼  
  1. //創建一個用於查詢的Query對象  
  2. Query query = userCache.createQuery();  
  3. Attribute<String> unitNo = userCache.getSearchAttribute("unitNo");  
  4. Attribute<Integer> age = userCache.getSearchAttribute("age");  
  5. //對單位編號進行分組  
  6. query.addGroupBy(unitNo);  
  7. //各單位年齡的平均值、最大值以及人數。  
  8. query.includeAggregator(age.average(), age.max(), age.count());  
  9. //查詢結果中還包含單位編碼  
  10. query.includeAttribute(unitNo);  
  11. Results results = query.execute();  
  12. List<Result> resultList = results.all();  
  13. if (resultList != null && !resultList.isEmpty()) {  
  14.    for (Result result : resultList) {  
  15.       String unitNoVal = result.getAttribute(unitNo);  
  16.       //多個統計信息將會組成一個List進行返回  
  17.       List<Object> aggregatorResults = result.getAggregatorResults();  
  18.       Number averageAge = (Number)aggregatorResults.get(0);  
  19.       Integer maxAge = (Integer)aggregatorResults.get(1);  
  20.       Integer count = (Integer)aggregatorResults.get(2);  
  21.       System.out.println("單位編號:" + unitNoVal + "---" + averageAge + "," + maxAge + "," + count);  
  22.    }  
  23. }  

 

3.7     讓Query不可變

       默認情況下,我們的Query可以在執行后修改某些屬性后繼續查詢。但是一旦我們調用了Query的end()方法之后我們將不能夠再更改Query的一些屬性。這包括調用include來定義返回結果中需要包含的信息、指定排序的屬性、指定分組的屬性、添加Criteria限制條件和調用maxResults()方法指定最大返回記錄數。

 

3.8     對BeanShell的支持

       BeanShell是用Java寫的,能夠對Java字符串表達式進行解釋執行的一個工具。如果在實際應用中我們需要讓用戶來自定義查詢的腳本時,我們就可以使用BeanShell來對查詢腳本進行解釋執行了。使用BeanShell前我們需加入BeanShell的jar包到類路徑,筆者下面的示例中使用的是BeanShell2.0的第4個測試版本。

Java代碼  
  1. @Test  
  2. public void beanShell() throws EvalError {  
  3.    CacheManager cacheManager = CacheManager.create();  
  4.    Cache userCache = cacheManager.getCache("userCache");  
  5.    User user;  
  6.    for (int i=0; i<10; i++) {  
  7.       user = new User(i, "name"+(i%2), 25+i);  
  8.       userCache.put(new Element(user.getId(), user));  
  9.    }  
  10.    //BeanShell解釋器,需引入BeanShell相關jar包  
  11.    Interpreter interpreter = new Interpreter();  
  12.    Query query = userCache.createQuery().includeValues();  
  13.    //Interpreter進行計算的字符串中出現的變量都需要放入Interpreter的環境中  
  14.    interpreter.set("query", query);//把query放入Interpreter環境中  
  15.    //把age放入Interpreter環境中  
  16.    interpreter.set("age", userCache.getSearchAttribute("age"));  
  17.    String queryStr = "query.addCriteria(age.lt(30)).execute();";  
  18.    //BeanShell執行字符串表達式對userCache進行查詢,並返回Results  
  19.    Results results = (Results)interpreter.eval(queryStr);  
  20.    for (Result result : results.all()) {  
  21.       System.out.println(result);  
  22.    }  
  23.    results.discard();  
  24. }  

 

       關於BeanShell的更多了解請訪問BeanShell的官方網站www.beanshell.org。

3.9     小結

       縱觀整個Ehcahce中對於Cache的查詢Query,我們可以發現其基本的邏輯和規則與SQL查詢是一樣的。可以進行篩選、選擇要查詢的結果、統計、排序和分組。Ehcache中的查詢也是先通過Criteria進行篩選,再進行分組和排序。

 

 

(注:本文是基於ehcache2.8.1所寫)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM