KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > cache > oscache > OSCacheController


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
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 JavaDoc getObject(CacheModel cacheModel, Object JavaDoc key) {
37     String JavaDoc keyString = key.toString();
38     try {
39       int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds());
40       return CACHE.getFromCache(keyString, refreshPeriod);
41     } catch (NeedsRefreshException e) {
42       CACHE.cancelUpdate(keyString);
43       return null;
44     }
45   }
46
47   public Object JavaDoc removeObject(CacheModel cacheModel, Object JavaDoc key) {
48     Object JavaDoc result;
49     String JavaDoc keyString = key.toString();
50     try {
51       int refreshPeriod = (int) (cacheModel.getFlushIntervalSeconds());
52       Object JavaDoc value = CACHE.getFromCache(keyString, refreshPeriod);
53       if (value != null) {
54         CACHE.flushEntry(keyString);
55       }
56       result = value;
57     } catch (NeedsRefreshException e) {
58       try {
59         CACHE.flushEntry(keyString);
60       } finally {
61         CACHE.cancelUpdate(keyString);
62         result = null;
63       }
64     }
65     return result;
66   }
67
68   public void putObject(CacheModel cacheModel, Object JavaDoc key, Object JavaDoc object) {
69     String JavaDoc keyString = key.toString();
70     CACHE.putInCache(keyString, object, new String JavaDoc[]{cacheModel.getId()});
71   }
72
73   public void configure(Properties JavaDoc props) {
74   }
75
76 }
77
Popular Tags