Java Reference
In-Depth Information
String key = "keyname";
String value = "valuename";
CacheStatistics stats = cache.getCacheStatistics();
int hits = stats.getCacheHits();
cache.put(key, value);
resp.getWriter().println("<br />value is " +
resp.getWriter().println("<br />hit count is " + hits);
Before you test the example, look at a few major sections of code in the preceding listing,
Listing 8-1. The first thing you'll notice is that you have just about as many import
statements as you do lines of code. There are no unused imports in the set. Listing 8-2
demonstrates how to query for the expiration of the cache.
Listing 8-2. Cache configuration settings
Map props = new HashMap();
props.put(GCacheFactory.EXPIRATION_DELTA, 3600);
props.put(MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT, true);
try {
CacheFactory cacheFactory =
cache = cacheFactory.createCache(props);
} catch (CacheException e) {
In Listing 8-2 you can see where the GCacheFactory class is being used to set the
expiration of the cache. As discussed earlier in this chapter, you can expire the cache
after a specific period of time has passed or at an absolute date and time. In this case,
you're using EXPIRATION_DELTA to set the cache to expire an hour after it's been set. The
available configuration options that control expiration are listed in Table 8-1.
Search WWH ::

Custom Search