Skip to content

Commit f262df3

Browse files
committed
some refactoring
1 parent b0e69ac commit f262df3

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/main/java/org/sharrissf/sample/EhcacheSearchPlaying.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,43 @@
1212
import net.sf.ehcache.search.Query;
1313
import net.sf.ehcache.search.Results;
1414
import net.sf.ehcache.search.aggregator.Average;
15+
import net.sf.ehcache.search.attribute.AttributeExtractor;
1516

1617
import org.sharrissf.sample.Person.Gender;
1718

19+
/**
20+
* Sample app briefly showing some of the api's one can use to search in Ehcache. This has been written against the latest snapshot build so
21+
* it can become outdated at any time
22+
*
23+
* @author steve
24+
*
25+
*/
1826
public class EhcacheSearchPlaying {
27+
private CacheManager cacheManager;
28+
private Cache cache;
1929

20-
public static void main(String[] args) throws IOException {
21-
22-
Configuration cacheManagerConfig = new Configuration();
30+
public EhcacheSearchPlaying() {
31+
initializeCache();
32+
}
2333

24-
// Add default cache
25-
cacheManagerConfig.addDefaultCache(new CacheConfiguration());
34+
private void initializeCache() {
2635

2736
// Create Cache
28-
37+
Configuration cacheManagerConfig = new Configuration();
38+
cacheManagerConfig.addDefaultCache(new CacheConfiguration());
2939
CacheConfiguration cacheConfig = new CacheConfiguration("test", -1).eternal(true);
3040

41+
// Create attributes on the stuff we want to be able to search on.
42+
43+
// You can use an expression for getting at the value to be indexed on a cache or you can code your own
3144
SearchAttribute sa = new SearchAttribute();
3245
sa.setExpression("value.getAge()");
3346
sa.setName("age");
3447
cacheConfig.addSearchAttribute(sa);
3548

3649
sa = new SearchAttribute();
37-
sa.setExpression("value.getName()");
50+
sa.className("org.sharrissf.sample.EhcacheSearchPlaying$NameAttributeExtractor");
51+
3852
sa.setName("name");
3953
cacheConfig.addSearchAttribute(sa);
4054

@@ -45,10 +59,11 @@ public static void main(String[] args) throws IOException {
4559

4660
cacheManagerConfig.addCache(cacheConfig);
4761

48-
CacheManager cacheManager = new CacheManager(cacheManagerConfig);
49-
50-
Cache cache = cacheManager.getCache("test");
62+
cacheManager = new CacheManager(cacheManagerConfig);
63+
cache = cacheManager.getCache("test");
64+
}
5165

66+
public void runTests() throws IOException {
5267
cache.put(new Element(1, new Person("Tim Eck", 35, Gender.MALE)));
5368
cache.put(new Element(2, new Person("Pamela Jones", 23, Gender.FEMALE)));
5469
cache.put(new Element(3, new Person("Ari Zilka", 25, Gender.MALE)));
@@ -82,12 +97,24 @@ public static void main(String[] args) throws IOException {
8297
Query averageAgeQuery = cache.createQuery();
8398
averageAgeQuery.includeAggregator(new Average(), age);
8499
System.out.println("Average age: " + averageAgeQuery.execute().aggregateResult());
85-
86100
}
87101

88102
private static void read() throws IOException {
89103
System.err.println("\nhit enter to continue");
90104
System.in.read();
91105
}
92106

107+
public static void main(String[] args) throws IOException {
108+
new EhcacheSearchPlaying().runTests();
109+
}
110+
111+
@SuppressWarnings( { "unused", "serial" })
112+
public static class NameAttributeExtractor implements AttributeExtractor {
113+
114+
@Override
115+
public Object attributeFor(Element element) {
116+
return ((Person) element.getValue()).getName();
117+
}
118+
119+
}
93120
}

0 commit comments

Comments
 (0)