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