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
17 package net.sf.ehcache.loader;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.Status;
22
23 import java.util.Collection;
24 import java.util.Map;
25 import java.util.Random;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /***
32 * A cache loader that counts the number of things it has loaded, useful for testing.
33 * This one returns null to all load methods.
34 * <p/>
35 * <p/>
36 * Each load has a random delay to introduce some nice threading entropy.
37 *
38 * @author Greg Luck
39 * @version $Id: NullCountingCacheLoader.html 13146 2011-08-01 17:12:39Z oletizi $
40 */
41 public class NullCountingCacheLoader implements CacheLoader {
42
43 private static final Logger LOG = LoggerFactory.getLogger(NullCountingCacheLoader.class.getName());
44
45 private volatile int loadCounter;
46 private volatile int loadAllCounter;
47 private Random random = new Random();
48 private String name = "CountingCacheLoader";
49
50 /***
51 * loads an object. Application writers should implement this
52 * method to customize the loading of cache object. This method is called
53 * by the caching service when the requested object is not in the cache.
54 * <p/>
55 *
56 * @param key the key identifying the object being loaded
57 * @return The object that is to be stored in the cache.
58 */
59 public Object load(Object key) throws CacheException {
60 try {
61 Thread.sleep(random.nextInt(3) + 1);
62 } catch (InterruptedException e) {
63 LOG.error("Interrupted");
64 }
65 loadCounter++;
66 return null;
67 }
68
69 /***
70 * loads multiple object. Application writers should implement this
71 * method to customize the loading of cache object. This method is called
72 * by the caching service when the requested object is not in the cache.
73 * <p/>
74 *
75 * @param keys a Collection of keys identifying the objects to be loaded
76 * @return A Map of objects that are to be stored in the cache.
77 */
78
79 public Map loadAll(Collection keys) throws CacheException {
80 loadAllCounter++;
81 return null;
82 }
83
84
85 /***
86 * @return
87 */
88 public int getLoadCounter() {
89 return loadCounter;
90 }
91
92 /***
93 * @return
94 */
95 public int getLoadAllCounter() {
96 return loadAllCounter;
97 }
98
99 /***
100 * Load using both a key and an argument.
101 * <p/>
102 * JCache will use the load(key) method where the argument is null.
103 *
104 * @param key
105 * @param argument
106 * @return
107 */
108 public Object load(Object key, Object argument) throws CacheException {
109 try {
110 Thread.sleep(random.nextInt(3) + 1);
111 } catch (InterruptedException e) {
112 LOG.error("Interrupted");
113 }
114 return name + ":" + argument;
115 }
116
117 /***
118 * Load using both a key and an argument.
119 * <p/>
120 * JCache will use the loadAll(key) method where the argument is null.
121 *
122 * @param keys
123 * @param argument
124 * @return
125 */
126 public Map loadAll(Collection keys, Object argument) throws CacheException {
127 return loadAll(keys);
128 }
129
130 /***
131 * Gets the name of a CacheLoader
132 *
133 * @return
134 */
135 public String getName() {
136 return name;
137 }
138
139 /***
140 * Creates a clone of this extension. This method will only be called by ehcache before a
141 * cache is initialized.
142 * <p/>
143 * Implementations should throw CloneNotSupportedException if they do not support clone
144 * but that will stop them from being used with defaultCache.
145 *
146 * @return a clone
147 * @throws CloneNotSupportedException if the extension could not be cloned.
148 */
149 public CacheLoader clone(Ehcache cache) throws CloneNotSupportedException {
150 return null;
151 }
152
153 /***
154 * Notifies providers to initialise themselves.
155 * <p/>
156 * This method is called during the Cache's initialise method after it has changed it's
157 * status to alive. Cache operations are legal in this method.
158 *
159 * @throws net.sf.ehcache.CacheException
160 */
161 public void init() {
162
163 }
164
165 /***
166 * Providers may be doing all sorts of exotic things and need to be able to clean up on
167 * dispose.
168 * <p/>
169 * Cache operations are illegal when this method is called. The cache itself is partly
170 * disposed when this method is called.
171 *
172 * @throws net.sf.ehcache.CacheException
173 */
174 public void dispose() throws net.sf.ehcache.CacheException {
175
176 }
177
178 /***
179 * @return the status of the extension
180 */
181 public Status getStatus() {
182 return null;
183 }
184
185 /***
186 * Sets the name
187 */
188 public void setName(String name) {
189 this.name = name;
190 }
191
192
193 }