View Javadoc

1   /*
2    * Copyright 2007 Hippo
3    *
4    * Licensed under the Apache License, Version 2.0 (the  "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" 
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  package nl.hippo.client.caching.service;
17  
18  import java.io.IOException;
19  import java.util.Iterator;
20  
21  import nl.hippo.client.api.caching.Cache;
22  import nl.hippo.client.api.caching.EventAwareCache;
23  import nl.hippo.client.api.event.EventAwareManager;
24  import nl.hippo.client.api.service.CachingService;
25  import nl.hippo.client.caching.CacheLogger;
26  import nl.hippo.client.caching.EventCache;
27  import nl.hippo.client.caching.EventCacheConfigBean;
28  
29  public class CachingServiceImpl implements CachingService {
30  
31      private Cache cache;
32  
33      public CachingServiceImpl(Cache cache) {
34          this.cache = cache;
35      }
36  
37      public CachingServiceImpl(Cache cache, EventAwareManager eventAwareManager, String namespace) {
38  
39          if (cache instanceof EventAwareCache) {
40              this.cache = cache;
41              eventAwareManager.addListener((EventAwareCache) cache, namespace);
42  
43          } else {
44              EventCacheConfigBean ecConfig = new EventCacheConfigBean();
45              ecConfig.setDelegate(cache);
46              EventAwareCache eventCache = new EventCache(ecConfig);
47  
48              this.cache = eventCache;
49              eventAwareManager.addListener(eventCache, namespace);
50          }
51      }
52  
53      public void store(Object key, Object obj) throws IOException {
54          if (CacheLogger.log.isDebugEnabled()) {
55              CacheLogger.log.debug("Caching object using CacheKey " + key.toString());
56          }
57          cache.store(key, obj);
58      }
59  
60      public void clear() {
61          CacheLogger.log.info("Clearing cache.");
62          cache.clear();
63      }
64  
65      public Object get(Object key) {
66          return cache.get(key);
67      }
68  
69      public void remove(Object key) {
70          if (CacheLogger.log.isDebugEnabled()) {
71              CacheLogger.log.debug("Removing cached entry stored under key " + key.toString());
72          }
73          cache.remove(key);
74      }
75  
76      public boolean containsKey(Object key) {
77          return cache.containsKey(key);
78      }
79  
80      public Iterator memoryKeys() {
81          return cache.memoryKeys();
82      }
83  
84      public int memorySize() {
85          return cache.memorySize();
86      }
87  
88      public String getStatisticsOverview() {
89          return cache.getStatisticsOverview();
90      }
91  
92  }