View Javadoc

1   /*
2    * TapestryCache.java
3    *
4    * Created on 16 Декабрь 2005 г., 12:24
5    *
6    * To change this template, choose Tools | Options and locate the template under
7    * the Source Creation and Management node. Right-click the template and choose
8    * Open. You can then make changes to the template in the Source Editor.
9    */
10  
11  package org.weda.cache.impl;
12  
13  import java.util.HashMap;
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.weda.cache.CacheEntity;
17  import org.weda.cache.CacheService;
18  
19  /**Простая реализация {@link tim.docarch.service.CacheService}. Объекты кеша
20   * храняться в HashMap.
21   *
22   * @author tim
23   */
24  public class SimpleCache implements CacheService, java.io.Serializable {
25      private final static Log log = LogFactory.getLog(SimpleCache.class);
26      private HashMap<String, CacheEntity> cache = new HashMap<String, CacheEntity>();
27      
28      public SimpleCache(){
29          if (log.isDebugEnabled())
30              log.debug("Creating new cache");
31      }
32      
33      public void release(String id) {
34          CacheEntity entity = cache.remove(id);
35          if (entity!=null)
36              entity.release();
37      }
38  
39      public CacheEntity get(String id) {
40          return cache.get(id);
41      }
42  
43      public void setCacheEntity(String id, CacheEntity entity) {
44          if (log.isDebugEnabled())
45              log.debug("Adding entity to cache: "+id);
46          cache.put(id, entity);
47      }
48  
49      public void releaseAll() {
50          for (CacheEntity entity: cache.values())
51              if (entity!=null)
52                  entity.release();
53          cache.clear();
54      }
55      
56  }