View Javadoc
1   package org.apache.commons.jcs3.access;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.jcs3.access.behavior.ICacheAccessManagement;
25  import org.apache.commons.jcs3.access.exception.CacheException;
26  import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
27  import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
28  import org.apache.commons.jcs3.engine.control.CompositeCache;
29  import org.apache.commons.jcs3.engine.stats.behavior.ICacheStats;
30  
31  /**
32   * This class provides the common methods for all types of access to the cache.
33   * <p>
34   * An instance of this class is tied to a specific cache region. Static methods are provided to get
35   * such instances.
36   * <p>
37   * Using this class you can retrieve an item, the item's wrapper, and the element's configuration.  You can also put an
38   * item in the cache, remove an item, and clear a region.
39   * <p>
40   * The JCS class is the preferred way to access these methods.
41   */
42  public abstract class AbstractCacheAccess<K, V>
43      implements ICacheAccessManagement
44  {
45      /**
46       * The cache that a given instance of this class provides access to.
47       * <p>
48       * TODO Should this be the interface?
49       */
50      private final CompositeCache<K, V> cacheControl;
51  
52      /**
53       * Constructor for the CacheAccess object.
54       * <p>
55       * @param cacheControl The cache which the created instance accesses
56       */
57      protected AbstractCacheAccess( final CompositeCache<K, V> cacheControl )
58      {
59          this.cacheControl = cacheControl;
60      }
61  
62      /**
63       * Removes all of the elements from a region.
64       * <p>
65       * @throws CacheException
66       */
67      @Override
68      public void clear()
69          throws CacheException
70      {
71          try
72          {
73              this.getCacheControl().removeAll();
74          }
75          catch ( final IOException e )
76          {
77              throw new CacheException( e );
78          }
79      }
80  
81      /**
82       * This method is does not reset the attributes for items already in the cache. It could
83       * potentially do this for items in memory, and maybe on disk (which would be slow) but not
84       * remote items. Rather than have unpredictable behavior, this method just sets the default
85       * attributes. Items subsequently put into the cache will use these defaults if they do not
86       * specify specific attributes.
87       * <p>
88       * @param attr the default attributes.
89       * @throws CacheException if something goes wrong.
90       */
91      @Override
92      public void setDefaultElementAttributes( final IElementAttributes attr )
93          throws CacheException
94      {
95          this.getCacheControl().setElementAttributes( attr );
96      }
97  
98      /**
99       * Retrieves A COPY OF the default element attributes used by this region. This does not provide
100      * a reference to the element attributes.
101      * <p>
102      * Each time an element is added to the cache without element attributes, the default element
103      * attributes are cloned.
104      * <p>
105      * @return the default element attributes used by this region.
106      * @throws CacheException
107      */
108     @Override
109     public IElementAttributes getDefaultElementAttributes()
110         throws CacheException
111     {
112         return this.getCacheControl().getElementAttributes();
113     }
114 
115     /**
116      * This returns the ICacheStats object with information on this region and its auxiliaries.
117      * <p>
118      * This data can be formatted as needed.
119      * <p>
120      * @return ICacheStats
121      */
122     @Override
123     public ICacheStats getStatistics()
124     {
125         return this.getCacheControl().getStatistics();
126     }
127 
128     /**
129      * @return A String version of the stats.
130      */
131     @Override
132     public String getStats()
133     {
134         return this.getCacheControl().getStats();
135     }
136 
137     /**
138      * Dispose this region. Flushes objects to and closes auxiliary caches. This is a shutdown
139      * command!
140      * <p>
141      * To simply remove all elements from the region use clear().
142      */
143     @Override
144     public void dispose()
145     {
146         this.getCacheControl().dispose();
147     }
148 
149     /**
150      * Gets the ICompositeCacheAttributes of the cache region.
151      * <p>
152      * @return ICompositeCacheAttributes, the controllers config info, defined in the top section of
153      *         a region definition.
154      */
155     @Override
156     public ICompositeCacheAttributes getCacheAttributes()
157     {
158         return this.getCacheControl().getCacheAttributes();
159     }
160 
161     /**
162      * Sets the ICompositeCacheAttributes of the cache region.
163      * <p>
164      * @param cattr The new ICompositeCacheAttribute value
165      */
166     @Override
167     public void setCacheAttributes( final ICompositeCacheAttributes cattr )
168     {
169         this.getCacheControl().setCacheAttributes( cattr );
170     }
171 
172     /**
173      * This instructs the memory cache to remove the <i>numberToFree</i> according to its eviction
174      * policy. For example, the LRUMemoryCache will remove the <i>numberToFree</i> least recently
175      * used items. These will be spooled to disk if a disk auxiliary is available.
176      * <p>
177      * @param numberToFree
178      * @return the number that were removed. if you ask to free 5, but there are only 3, you will
179      *         get 3.
180      * @throws CacheException
181      */
182     @Override
183     public int freeMemoryElements( final int numberToFree )
184         throws CacheException
185     {
186         int numFreed = -1;
187         try
188         {
189             numFreed = this.getCacheControl().getMemoryCache().freeElements( numberToFree );
190         }
191         catch ( final IOException ioe )
192         {
193             final String message = "Failure freeing memory elements.";
194             throw new CacheException( message, ioe );
195         }
196         return numFreed;
197     }
198 
199     public CompositeCache<K, V> getCacheControl() {
200         return cacheControl;
201     }
202 
203 }