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.store;
18
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.config.CacheConfiguration;
22 import net.sf.ehcache.pool.Pool;
23 import net.sf.ehcache.store.disk.DiskStore;
24
25 /***
26 * A tiered store using an in-memory cache of elements stored on disk.
27 *
28 * @author Ludovic Orban
29 */
30 public final class DiskBackedMemoryStore extends FrontEndCacheTier<MemoryStore, DiskStore> {
31
32 private DiskBackedMemoryStore(CacheConfiguration cacheConfiguration, MemoryStore cache, DiskStore authority) {
33 super(cache, authority, cacheConfiguration.getCopyStrategy(), cacheConfiguration.isCopyOnWrite(), cacheConfiguration.isCopyOnRead());
34 }
35
36 /***
37 * Create a DiskBackedMemoryStore instance
38 * @param cache the cache
39 * @param diskStorePath the path to the folder in which files will be created
40 * @param onHeapPool the pool tracking on-heap usage
41 * @param onDiskPool the pool tracking on-disk usage
42 * @return a DiskBackedMemoryStore instance
43 */
44 public static Store create(Ehcache cache, String diskStorePath, Pool onHeapPool, Pool onDiskPool) {
45 final MemoryStore memoryStore = createMemoryStore(cache, onHeapPool);
46 DiskStore diskStore = createDiskStore(cache, diskStorePath, onHeapPool, onDiskPool);
47
48 return new DiskBackedMemoryStore(cache.getCacheConfiguration(), memoryStore, diskStore);
49 }
50
51 private static MemoryStore createMemoryStore(Ehcache cache, Pool onHeapPool) {
52 return MemoryStore.create(cache, onHeapPool);
53 }
54
55 private static DiskStore createDiskStore(Ehcache cache, String diskPath, Pool onHeapPool, Pool onDiskPool) {
56 CacheConfiguration config = cache.getCacheConfiguration();
57 if (config.isDiskPersistent() || config.isOverflowToDisk()) {
58 return DiskStore.create(cache, diskPath, onHeapPool, onDiskPool);
59 } else {
60 throw new CacheException("DiskBackedMemoryStore can only be used when cache overflows to disk or is disk persistent");
61 }
62 }
63
64 /***
65 * {@inheritDoc}
66 */
67 public Object getMBean() {
68 return null;
69 }
70 }