KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > adaptor > JcsCache


1 package org.shiftone.cache.adaptor;
2
3
4
5 import org.apache.commons.lang.exception.NestableRuntimeException;
6 import org.apache.jcs.JCS;
7 import org.apache.jcs.access.CacheAccess;
8 import org.apache.jcs.access.exception.CacheException;
9 import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
10 import org.shiftone.cache.Cache;
11
12
13 /**
14  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
15  * @version $Revision: 1.9 $
16  */

17 public class JcsCache implements Cache
18 {
19
20     private final CacheAccess cache;
21
22     public JcsCache()
23     {
24         this("default");
25     }
26
27     public JcsCache(CacheAccess access)
28     {
29         this.cache = access;
30     }
31
32     public JcsCache(String JavaDoc name)
33     {
34
35         try
36         {
37             cache = JCS.getInstance(name);
38         }
39         catch (CacheException e)
40         {
41             throw new NestableRuntimeException("new JcsCache : " + name, e);
42         }
43     }
44
45
46     public void addObject(Object JavaDoc userKey, Object JavaDoc cacheObject)
47     {
48
49         try
50         {
51             cache.put(userKey, cacheObject);
52         }
53         catch (CacheException e)
54         {
55             throw new NestableRuntimeException("addObject", e);
56         }
57     }
58
59
60     public Object JavaDoc getObject(Object JavaDoc key)
61     {
62         return cache.get(key);
63     }
64
65
66     /**
67      * NOOP
68      */

69     public int size()
70     {
71         return -1;
72     }
73
74
75     public void remove(Object JavaDoc key)
76     {
77
78         try
79         {
80             cache.remove(key);
81         }
82         catch (CacheException e)
83         {
84             throw new NestableRuntimeException("remove", e);
85         }
86     }
87
88
89     public void clear()
90     {
91
92         try
93         {
94             cache.remove();
95         }
96         catch (CacheException e)
97         {
98             throw new NestableRuntimeException("remove", e);
99         }
100     }
101
102
103     public String JavaDoc toString()
104     {
105
106         ICompositeCacheAttributes attributes = cache.getCacheAttributes();
107
108         return "JCS[" + attributes.getCacheName() //
109
+ ":" + attributes.getMemoryCacheName() //
110
+ ":" + attributes.getMaxObjects() + "]";
111     }
112 }
113
Popular Tags