View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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 net.sf.ehcache.hibernate.strategy;
17  
18  import net.sf.ehcache.hibernate.regions.EhcacheEntityRegion;
19  
20  import org.hibernate.cache.CacheException;
21  import org.hibernate.cache.EntityRegion;
22  import org.hibernate.cache.access.EntityRegionAccessStrategy;
23  import org.hibernate.cache.access.SoftLock;
24  import org.hibernate.cfg.Settings;
25  
26  /***
27   * Ehcache specific non-strict read/write entity region access strategy
28   *
29   * @author Chris Dennis
30   */
31  public class NonStrictReadWriteEhcacheEntityRegionAccessStrategy extends AbstractEhcacheAccessStrategy<EhcacheEntityRegion>
32          implements EntityRegionAccessStrategy {
33  
34      /***
35       * Create a non-strict read/write access strategy accessing the given collection region.
36       */   
37      public NonStrictReadWriteEhcacheEntityRegionAccessStrategy(EhcacheEntityRegion region, Settings settings) {
38          super(region, settings);
39      }
40  
41      /***
42       * {@inheritDoc}
43       */
44      public EntityRegion getRegion() {
45          return region;
46      }
47  
48      /***
49       * {@inheritDoc}
50       */
51      public Object get(Object key, long txTimestamp) throws CacheException {
52          return region.get(key);
53      }
54  
55      /***
56       * {@inheritDoc}
57       */
58      public boolean putFromLoad(Object key, Object value, long txTimestamp, Object version, boolean minimalPutOverride)
59              throws CacheException {
60          if (minimalPutOverride && region.contains(key)) {
61              return false;
62          } else {
63              region.put(key, value);
64              return true;
65          }
66      }
67  
68      /***
69       * Since this is a non-strict read/write strategy item locking is not used.
70       */
71      public SoftLock lockItem(Object key, Object version) throws CacheException {
72          return null;
73      }
74  
75      /***
76       * Since this is a non-strict read/write strategy item locking is not used.
77       */
78      public void unlockItem(Object key, SoftLock lock) throws CacheException {
79          region.remove(key);
80      }
81  
82      /***
83       * Returns <code>false</code> since this is an asynchronous cache access strategy.
84       */
85      public boolean insert(Object key, Object value, Object version) throws CacheException {
86          return false;
87      }
88  
89      /***
90       * Returns <code>false</code> since this is a non-strict read/write cache access strategy
91       */
92      public boolean afterInsert(Object key, Object value, Object version) throws CacheException {
93          return false;
94      }
95  
96      /***
97       * Removes the entry since this is a non-strict read/write cache strategy.
98       */
99      public boolean update(Object key, Object value, Object currentVersion, Object previousVersion) throws CacheException {
100         remove(key);
101         return false;
102     }
103 
104     /***
105      * {@inheritDoc}
106      */
107     public boolean afterUpdate(Object key, Object value, Object currentVersion, Object previousVersion, SoftLock lock)
108             throws CacheException {
109         unlockItem(key, lock);
110         return false;
111     }
112 
113     /***
114      * {@inheritDoc}
115      */
116     @Override
117     public void remove(Object key) throws CacheException {
118         region.remove(key);
119     }
120 }