01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * 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 com.ibatis.sqlmap.engine.cache.oscache;
17:
18: import com.ibatis.sqlmap.engine.cache.CacheController;
19: import com.ibatis.sqlmap.engine.cache.CacheModel;
20: import com.opensymphony.oscache.base.NeedsRefreshException;
21: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
22:
23: import java.util.Properties;
24:
25: /**
26: * Cache implementation for using OSCache with iBATIS
27: */
28: public class OSCacheController implements CacheController {
29:
30: private static final GeneralCacheAdministrator CACHE = new GeneralCacheAdministrator();
31:
32: public void flush(CacheModel cacheModel) {
33: CACHE.flushGroup(cacheModel.getId());
34: }
35:
36: public Object getObject(CacheModel cacheModel, Object key) {
37: String keyString = key.toString();
38: try {
39: int refreshPeriod = (int) (cacheModel
40: .getFlushIntervalSeconds());
41: return CACHE.getFromCache(keyString, refreshPeriod);
42: } catch (NeedsRefreshException e) {
43: CACHE.cancelUpdate(keyString);
44: return null;
45: }
46: }
47:
48: public Object removeObject(CacheModel cacheModel, Object key) {
49: Object result;
50: String keyString = key.toString();
51: try {
52: int refreshPeriod = (int) (cacheModel
53: .getFlushIntervalSeconds());
54: Object value = CACHE.getFromCache(keyString, refreshPeriod);
55: if (value != null) {
56: CACHE.flushEntry(keyString);
57: }
58: result = value;
59: } catch (NeedsRefreshException e) {
60: try {
61: CACHE.flushEntry(keyString);
62: } finally {
63: CACHE.cancelUpdate(keyString);
64: result = null;
65: }
66: }
67: return result;
68: }
69:
70: public void putObject(CacheModel cacheModel, Object key,
71: Object object) {
72: String keyString = key.toString();
73: CACHE.putInCache(keyString, object, new String[] { cacheModel
74: .getId() });
75: }
76:
77: public void configure(Properties props) {
78: }
79:
80: }
|