01: package org.shiftone.cache.adaptor;
02:
03: import org.apache.commons.lang.exception.NestableRuntimeException;
04: import org.apache.jcs.JCS;
05: import org.apache.jcs.access.CacheAccess;
06: import org.apache.jcs.access.exception.CacheException;
07: import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
08: import org.shiftone.cache.Cache;
09:
10: /**
11: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
12: * @version $Revision: 1.9 $
13: */
14: public class JcsCache implements Cache {
15:
16: private final CacheAccess cache;
17:
18: public JcsCache() {
19: this ("default");
20: }
21:
22: public JcsCache(CacheAccess access) {
23: this .cache = access;
24: }
25:
26: public JcsCache(String name) {
27:
28: try {
29: cache = JCS.getInstance(name);
30: } catch (CacheException e) {
31: throw new NestableRuntimeException(
32: "new JcsCache : " + name, e);
33: }
34: }
35:
36: public void addObject(Object userKey, Object cacheObject) {
37:
38: try {
39: cache.put(userKey, cacheObject);
40: } catch (CacheException e) {
41: throw new NestableRuntimeException("addObject", e);
42: }
43: }
44:
45: public Object getObject(Object key) {
46: return cache.get(key);
47: }
48:
49: /**
50: * NOOP
51: */
52: public int size() {
53: return -1;
54: }
55:
56: public void remove(Object key) {
57:
58: try {
59: cache.remove(key);
60: } catch (CacheException e) {
61: throw new NestableRuntimeException("remove", e);
62: }
63: }
64:
65: public void clear() {
66:
67: try {
68: cache.remove();
69: } catch (CacheException e) {
70: throw new NestableRuntimeException("remove", e);
71: }
72: }
73:
74: public String toString() {
75:
76: ICompositeCacheAttributes attributes = cache
77: .getCacheAttributes();
78:
79: return "JCS[" + attributes.getCacheName() //
80: + ":" + attributes.getMemoryCacheName() //
81: + ":" + attributes.getMaxObjects() + "]";
82: }
83: }
|