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.java 2154 2010-04-06 02:45:52Z cdennis $
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 */
61 public Object load(Object key) throws CacheException {
62 try {
63 Thread.sleep(random.nextInt(3) + 1);
64 } catch (InterruptedException e) {
65 LOG.error("Interrupted");
66 }
67 return Integer.valueOf(loadCounter++);
68 }
69
70 /***
71 * loads multiple object. Application writers should implement this
72 * method to customize the loading of cache object. This method is called
73 * by the caching service when the requested object is not in the cache.
74 * <p/>
75 *
76 * @param keys a Collection of keys identifying the objects to be loaded
77 * @return A Map of objects that are to be stored in the cache.
78 *
79 */
80
81 public Map loadAll(Collection keys) throws CacheException {
82 Map map = new HashMap(keys.size());
83 for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
84 Object key = iterator.next();
85 try {
86 Thread.sleep(random.nextInt(4));
87 } catch (InterruptedException e) {
88 LOG.error("Interrupted");
89 }
90 map.put(key, Integer.valueOf(loadAllCounter++));
91 }
92 return map;
93 }
94
95
96 /***
97 * @return
98 */
99 public int getLoadCounter() {
100 return loadCounter;
101 }
102
103 /***
104 * @return
105 */
106 public int getLoadAllCounter() {
107 return loadAllCounter;
108 }
109
110 /***
111 * Load using both a key and an argument.
112 * <p/>
113 * JCache will use the load(key) method where the argument is null.
114 *
115 * @param key
116 * @param argument
117 * @return
118 *
119 */
120 public Object load(Object key, Object argument) throws CacheException {
121 try {
122 Thread.sleep(random.nextInt(3) + 1);
123 } catch (InterruptedException e) {
124 LOG.error("Interrupted");
125 }
126 return name + ":" + argument;
127 }
128
129 /***
130 * Load using both a key and an argument.
131 * <p/>
132 * JCache will use the loadAll(key) method where the argument is null.
133 *
134 * @param keys
135 * @param argument
136 * @return
137 *
138 */
139 public Map loadAll(Collection keys, Object argument) throws CacheException {
140 return loadAll(keys);
141 }
142
143 /***
144 * Gets the name of a CacheLoader
145 *
146 * @return
147 */
148 public String getName() {
149 return name;
150 }
151
152 /***
153 * Creates a clone of this extension. This method will only be called by ehcache before a
154 * cache is initialized.
155 * <p/>
156 * Implementations should throw CloneNotSupportedException if they do not support clone
157 * but that will stop them from being used with defaultCache.
158 *
159 * @return a clone
160 * @throws CloneNotSupportedException if the extension could not be cloned.
161 */
162 public CacheLoader clone(Ehcache cache) throws CloneNotSupportedException {
163 return null;
164 }
165
166 /***
167 * Notifies providers to initialise themselves.
168 * <p/>
169 * This method is called during the Cache's initialise method after it has changed it's
170 * status to alive. Cache operations are legal in this method.
171 *
172 * @throws net.sf.ehcache.CacheException
173 */
174 public void init() {
175
176 }
177
178 /***
179 * Providers may be doing all sorts of exotic things and need to be able to clean up on
180 * dispose.
181 * <p/>
182 * Cache operations are illegal when this method is called. The cache itself is partly
183 * disposed when this method is called.
184 *
185 * @throws net.sf.ehcache.CacheException
186 */
187 public void dispose() throws net.sf.ehcache.CacheException {
188
189 }
190
191 /***
192 * @return the status of the extension
193 */
194 public Status getStatus() {
195 return null;
196 }
197
198 /***
199 * Sets the name
200 */
201 public void setName(String name) {
202 this.name = name;
203 }
204
205
206 }