View Javadoc
1   /**
2    *    Copyright 2010-2016 the original author or authors.
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 org.mybatis.caches.oscache;
17  
18  import java.util.concurrent.locks.ReadWriteLock;
19  import java.util.concurrent.locks.ReentrantReadWriteLock;
20  
21  import org.apache.ibatis.cache.Cache;
22  
23  import com.opensymphony.oscache.base.NeedsRefreshException;
24  import com.opensymphony.oscache.general.GeneralCacheAdministrator;
25  
26  /**
27   * The OSCache-based Cache implementation.
28   */
29  public final class OSCache implements Cache {
30  
31      /**
32       * The shared 
33       */
34      private static final GeneralCacheAdministrator CACHE_ADMINISTRATOR = new GeneralCacheAdministrator();
35  
36      /**
37       * The {@code ReadWriteLock}.
38       */
39      private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
40  
41      /**
42       * This cache id.
43       */
44      private final String id;
45  
46      /**
47       * Builds a new OSCache-based Cache.
48       *
49       * @param id the Mapper id.
50       */
51      public OSCache(final String id) {
52          if (id == null) {
53              throw new IllegalArgumentException("Cache instances require an ID");
54          }
55          this.id = id;
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public void clear() {
63          CACHE_ADMINISTRATOR.flushGroup(this.id);
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public String getId() {
71          return this.id;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public Object getObject(Object key) {
79          String keyString = key.toString();
80          Object ret = null;
81  
82          try {
83              ret = CACHE_ADMINISTRATOR.getFromCache(keyString);
84          } catch (NeedsRefreshException e) {
85              CACHE_ADMINISTRATOR.cancelUpdate(keyString);
86          }
87  
88          return ret;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public ReadWriteLock getReadWriteLock() {
96          return this.readWriteLock;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public int getSize() {
104         return CACHE_ADMINISTRATOR.getCache().getSize();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void putObject(Object key, Object value) {
112         CACHE_ADMINISTRATOR.putInCache(key.toString(), value, new String[] { this.id });
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public Object removeObject(Object key) {
120         String keyString = key.toString();
121         Object ret = null;
122 
123         try {
124             ret = CACHE_ADMINISTRATOR.getFromCache(keyString);
125         } catch (NeedsRefreshException e) {
126             CACHE_ADMINISTRATOR.cancelUpdate(keyString);
127         }
128 
129         if (ret != null) {
130             CACHE_ADMINISTRATOR.flushEntry(keyString);
131         }
132 
133         return ret;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public boolean equals(Object obj) {
141         if (this == obj) {
142             return true;
143         }
144         if (obj == null) {
145             return false;
146         }
147         if (!(obj instanceof Cache)) {
148             return false;
149         }
150 
151         Cache otherCache = (Cache) obj;
152         return this.id.equals(otherCache.getId());
153     }
154 
155     /**
156      * {@inheritDoc}
157      */
158     @Override
159     public int hashCode() {
160         return this.id.hashCode();
161     }
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public String toString() {
168         return "OSCache {"
169                 + this.id
170                 + "}";
171     }
172 
173 }