2
2
3
3
import java .io .IOException ;
4
4
5
- import net .sf .ehcache .Cache ;
6
5
import net .sf .ehcache .CacheManager ;
6
+ import net .sf .ehcache .Ehcache ;
7
7
import net .sf .ehcache .Element ;
8
8
import net .sf .ehcache .config .CacheConfiguration ;
9
9
import net .sf .ehcache .config .Configuration ;
12
12
import net .sf .ehcache .search .Query ;
13
13
import net .sf .ehcache .search .Results ;
14
14
import net .sf .ehcache .search .aggregator .Average ;
15
+ import net .sf .ehcache .search .aggregator .Count ;
15
16
import net .sf .ehcache .search .attribute .AttributeExtractor ;
17
+ import net .sf .ehcache .search .expression .And ;
16
18
17
19
import org .sharrissf .sample .Person .Gender ;
18
20
25
27
*/
26
28
public class EhcacheSearchPlaying {
27
29
private CacheManager cacheManager ;
28
- private Cache cache ;
30
+ private Ehcache cache ;
29
31
30
32
public EhcacheSearchPlaying () {
31
33
initializeCache ();
@@ -48,7 +50,6 @@ private void initializeCache() {
48
50
49
51
sa = new SearchAttribute ();
50
52
sa .className ("org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor" );
51
-
52
53
sa .setName ("name" );
53
54
cacheConfig .addSearchAttribute (sa );
54
55
@@ -57,46 +58,72 @@ private void initializeCache() {
57
58
sa .setName ("gender" );
58
59
cacheConfig .addSearchAttribute (sa );
59
60
61
+ sa = new SearchAttribute ();
62
+ sa .setExpression ("value.getAddress().getState()" );
63
+ sa .setName ("state" );
64
+ cacheConfig .addSearchAttribute (sa );
65
+
60
66
cacheManagerConfig .addCache (cacheConfig );
61
67
62
68
cacheManager = new CacheManager (cacheManagerConfig );
63
- cache = cacheManager .getCache ("test" );
69
+ cache = cacheManager .getEhcache ("test" );
64
70
}
65
71
66
72
public void runTests () throws IOException {
67
- cache .put (new Element (1 , new Person ("Tim Eck" , 35 , Gender .MALE )));
68
- cache .put (new Element (2 , new Person ("Pamela Jones" , 23 , Gender .FEMALE )));
69
- cache .put (new Element (3 , new Person ("Ari Zilka" , 25 , Gender .MALE )));
70
- cache .put (new Element (4 , new Person ("Ari gold" , 45 , Gender .MALE )));
71
- cache .put (new Element (5 , new Person ("Nabib El-Rahman" , 30 , Gender .MALE )));
72
- for (int i = 5 ; i < 1000 ; i ++) {
73
- cache .put (new Element (i , new Person ("Nabib El-Rahman" + i , 30 , Gender .MALE )));
74
- }
73
+ loadCache ();
75
74
76
75
Attribute <Integer > age = cache .getSearchAttribute ("age" );
77
76
Attribute <Gender > gender = cache .getSearchAttribute ("gender" );
78
77
Attribute <String > name = cache .getSearchAttribute ("name" );
78
+ Attribute <String > state = cache .getSearchAttribute ("state" );
79
79
80
80
Query query = cache .createQuery ();
81
81
query .includeKeys ();
82
- query .add (name .like ("Ari*" ));
82
+ query .add (new And ( name .like ("Ari*" ), gender . eq ( Gender . MALE ) ));
83
83
84
84
long t = System .currentTimeMillis ();
85
+ System .out .println ("Searching for all Person's who's name start with Ari and are Male:" );
86
+
85
87
Results results = query .execute ();
86
88
System .out .println ("Took: " + (System .currentTimeMillis () - t ) + " Size: " + results .size ());
87
89
88
90
read ();
89
91
90
- cache . put ( new Element ( 1 , new Person ( "Tim Eck" , 36 , Gender . MALE )) );
92
+ System . out . println ( "Adding another Ari" );
91
93
92
- t = System . currentTimeMillis ( );
94
+ cache . put ( new Element ( 1 , new Person ( "Ari Eck" , 36 , Gender . MALE , "eck street" , "San Mateo" , "CA" )) );
93
95
96
+ t = System .currentTimeMillis ();
97
+ System .out .println ("Again Searching for all Person's who's name start with Ari and are Male:" );
94
98
results = query .execute ();
95
99
System .out .println ("Took: " + (System .currentTimeMillis () - t ) + " Size: " + results .size ());
96
100
101
+ read ();
102
+
103
+ System .out .println ("Find the average age of all the entries in the cache" );
104
+
97
105
Query averageAgeQuery = cache .createQuery ();
98
106
averageAgeQuery .includeAggregator (new Average (), age );
99
107
System .out .println ("Average age: " + averageAgeQuery .execute ().aggregateResult ());
108
+
109
+ read ();
110
+
111
+ System .out .println ("Find the count of people from NJ" );
112
+
113
+ Query newJerseyCountQuery = cache .createQuery ().add (state .eq ("NJ" ));
114
+ newJerseyCountQuery .includeAggregator (new Count (), state );
115
+ System .out .println ("Count of people from NJ: " + newJerseyCountQuery .execute ().aggregateResult ());
116
+ }
117
+
118
+ private void loadCache () {
119
+ cache .put (new Element (1 , new Person ("Tim Eck" , 35 , Gender .MALE , "eck street" , "San Mateo" , "CA" )));
120
+ cache .put (new Element (2 , new Person ("Pamela Jones" , 23 , Gender .FEMALE , "berry st" , "Parsippany" , "LA" )));
121
+ cache .put (new Element (3 , new Person ("Ari Zilka" , 25 , Gender .MALE , "big wig" , "Beverly Hills" , "NJ" )));
122
+ cache .put (new Element (4 , new Person ("Ari gold" , 45 , Gender .MALE , "cool agent" , "Madison" , "WI" )));
123
+ cache .put (new Element (5 , new Person ("Nabib El-Rahman" , 30 , Gender .MALE , "dah man" , "Bangladesh" , "MN" )));
124
+ for (int i = 5 ; i < 1000 ; i ++) {
125
+ cache .put (new Element (i , new Person ("Nabib El-Rahman" + i , 30 , Gender .MALE , "dah man" , "Bangladesh" , "NJ" )));
126
+ }
100
127
}
101
128
102
129
private static void read () throws IOException {
@@ -108,9 +135,13 @@ public static void main(String[] args) throws IOException {
108
135
new EhcacheSearchPlaying ().runTests ();
109
136
}
110
137
111
- @ SuppressWarnings ( { "unused" , "serial" })
112
138
public static class NameAttributeExtractor implements AttributeExtractor {
113
139
140
+ /**
141
+ *
142
+ */
143
+ private static final long serialVersionUID = 1L ;
144
+
114
145
@ Override
115
146
public Object attributeFor (Element element ) {
116
147
return ((Person ) element .getValue ()).getName ();
0 commit comments