KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cache4j > perfomance > cache > oscache


1 /* =========================================================================
2  * File: oscache.java$
3  *
4  * Copyright 2006 by Yuriy Stepovoy.
5  * email: stepovoy@gmail.com
6  * All rights reserved.
7  *
8  * =========================================================================
9  */

10
11 package net.sf.cache4j.perfomance.cache;
12
13 import net.sf.cache4j.perfomance.ICache;
14 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
15
16 import java.util.Properties JavaDoc;
17
18 import org.apache.log4j.PropertyConfigurator;
19
20 /**
21  * oscache
22  *
23  * @version $Revision: 1.0 $ $Date: 04/03/2003 11:00:00 $
24  * @author Yuriy Stepovoy. <a HREF="mailto:stepovoy@gmail.com">stepovoy@gmail.com</a>
25  **/

26
27 public class oscache implements ICache {
28 // ----------------------------------------------------------------------------- Константы
29
// ----------------------------------------------------------------------------- Атрибуты класса
30
private GeneralCacheAdministrator _cache;
31 // ----------------------------------------------------------------------------- Статические переменные
32

33     public static final String JavaDoc NAME = "oscache 2.2";
34
35 // ----------------------------------------------------------------------------- Конструкторы
36
// ----------------------------------------------------------------------------- Public методы
37

38     /**
39      * Инициализирует кеш
40      * @throws Exception
41      */

42     public void init() throws Exception JavaDoc {
43         Properties JavaDoc prop = new Properties JavaDoc();
44         prop.put("log4j.logger.com.opensymphony.oscache", "fatal, oscache");
45         prop.put("log4j.appender.oscache", "org.apache.log4j.ConsoleAppender");
46         prop.put("log4j.appender.oscache.layout", "org.apache.log4j.PatternLayout");
47         prop.put("log4j.appender.oscache.layout.ConversionPattern", "%d %-5p%m%n");
48         PropertyConfigurator.configure(prop);
49
50         prop = new Properties JavaDoc();
51         prop.put("cache.memory", "true");
52         prop.put("cache.use.host.domain.in.key", "false");
53         prop.put("cache.algorithm", "com.opensymphony.oscache.base.algorithm.LRUCache");
54         prop.put("cache.capacity", "1000");
55         _cache = new GeneralCacheAdministrator(prop);
56     }
57
58     /**
59      * Возвращает объект из кеша
60      * @param id идентификатор объекта
61      * @return
62      * @throws Exception
63      */

64     public Object JavaDoc get(Object JavaDoc id) throws Exception JavaDoc {
65         try {
66             return _cache.getFromCache(id.toString());
67         } catch (Exception JavaDoc ex) {
68             try {
69                 _cache.cancelUpdate(id.toString());
70 /*
71 java.lang.RuntimeException: java.lang.IllegalStateException: Cannot cancel cache update - current state (2) is not UPDATE_IN_PROGRESS
72         at net.sf.cache4j.perfomance.test.GetPutRemoveThread$TThread.run(GetPutRemoveThread.java:125)
73 Caused by: java.lang.IllegalStateException: Cannot cancel cache update - current state (2) is not UPDATE_IN_PROGRESS
74         at com.opensymphony.oscache.base.EntryUpdateState.cancelUpdate(EntryUpdateState.java:91)
75         at com.opensymphony.oscache.base.Cache.cancelUpdate(Cache.java:378)
76         at com.opensymphony.oscache.general.GeneralCacheAdministrator.cancelUpdate(GeneralCacheAdministrator.java:184)
77         at net.sf.cache4j.perfomance.cache.oscache.put(oscache.java:89)
78         at net.sf.cache4j.perfomance.test.GetPutRemoveThread$TThread.run(GetPutRemoveThread.java:118)
79 */

80             } catch (Exception JavaDoc e){ }
81         }
82         return null;
83     }
84
85     /**
86      * Помещает объект в кеш
87      * @param id идентификатор объекта
88      * @param value объект
89      * @throws Exception
90      */

91     public void put(Object JavaDoc id, Object JavaDoc value) throws Exception JavaDoc {
92         boolean updated = false;
93         try {
94             _cache.putInCache(id.toString(), value);
95             updated = true;
96         } catch (Exception JavaDoc ex) {
97             if (!updated) {
98                 try {
99                     _cache.cancelUpdate(id.toString());
100 /*
101 java.lang.RuntimeException: java.lang.IllegalStateException: Cannot cancel cache update - current state (2) is not UPDATE_IN_PROGRESS
102         at net.sf.cache4j.perfomance.test.GetPutRemoveThread$TThread.run(GetPutRemoveThread.java:125)
103 Caused by: java.lang.IllegalStateException: Cannot cancel cache update - current state (2) is not UPDATE_IN_PROGRESS
104         at com.opensymphony.oscache.base.EntryUpdateState.cancelUpdate(EntryUpdateState.java:91)
105         at com.opensymphony.oscache.base.Cache.cancelUpdate(Cache.java:378)
106         at com.opensymphony.oscache.general.GeneralCacheAdministrator.cancelUpdate(GeneralCacheAdministrator.java:184)
107         at net.sf.cache4j.perfomance.cache.oscache.put(oscache.java:89)
108         at net.sf.cache4j.perfomance.test.GetPutRemoveThread$TThread.run(GetPutRemoveThread.java:118)
109 */

110                 } catch (Exception JavaDoc e){ }
111             }
112         }
113     }
114
115     /**
116      * Удаляет обюъект из кеша
117      * @param id идентификатор объекта
118      * @throws Exception
119      */

120     public void remove(Object JavaDoc id) throws Exception JavaDoc {
121         _cache.flushEntry(id.toString());
122     }
123
124     /**
125      * Возвращает количество объектов в кеше
126      * @return
127      * @throws Exception
128      */

129     public long size() throws Exception JavaDoc {
130         return 0;
131     }
132
133     /**
134      * Деинициализирует кеш
135      */

136     public void destroy() {
137         _cache = null;
138     }
139
140     /**
141      * Возвращает название кеша
142      * @return
143      */

144     public String JavaDoc getCacheName() {
145         return NAME;
146     }
147
148 // ----------------------------------------------------------------------------- Package scope методы
149
// ----------------------------------------------------------------------------- Protected методы
150
// ----------------------------------------------------------------------------- Private методы
151
// ----------------------------------------------------------------------------- Inner классы
152
}
Popular Tags