View Javadoc

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;
18  
19  import junit.framework.Assert;
20  import net.sf.ehcache.config.CacheConfiguration;
21  import net.sf.ehcache.config.Configuration;
22  import net.sf.ehcache.config.DiskStoreConfiguration;
23  import net.sf.ehcache.config.PinningConfiguration;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertNotNull;
29  
30  /***
31   * Tests for Cache pinning
32   *
33   * @author Ludovic Orban
34   */
35  public class CachePinningTest {
36  
37      private static final int ELEMENT_COUNT = 40000;
38  
39      private CacheManager cacheManager;
40  
41      @Before
42      public void setUp() throws Exception {
43          cacheManager = new CacheManager(
44                  new Configuration()
45                          .diskStore(new DiskStoreConfiguration().path(System.getProperty("java.io.tmpdir")))
46          );
47      }
48  
49      @After
50      public void tearDown() {
51          cacheManager.shutdown();
52          cacheManager = null;
53      }
54  
55      @Test
56      public void testClassicLru() throws Exception {
57          System.setProperty(Cache.NET_SF_EHCACHE_USE_CLASSIC_LRU, "true");
58          try {
59              testMemoryOnly();
60          } finally {
61              System.setProperty(Cache.NET_SF_EHCACHE_USE_CLASSIC_LRU, "false");
62          }
63      }
64  
65      @Test
66      public void testMemoryOnly() throws Exception {
67          cacheManager.addCache(new Cache(
68                  new CacheConfiguration()
69                          .maxElementsInMemory(10)
70                          .statistics(true)
71                          .name("memoryOnlyCache_onHeap")
72                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
73          ));
74          doAssertions(cacheManager.getCache("memoryOnlyCache_onHeap"), ELEMENT_COUNT, 0);
75  
76          cacheManager.addCache(new Cache(
77                  new CacheConfiguration()
78                          .maxElementsInMemory(10)
79                          .statistics(true)
80                          .name("memoryOnlyCache_inMemory")
81                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
82          ));
83          doAssertions(cacheManager.getCache("memoryOnlyCache_inMemory"), ELEMENT_COUNT, 0);
84  
85          cacheManager.addCache(new Cache(
86                  new CacheConfiguration()
87                          .maxElementsInMemory(10)
88                          .statistics(true)
89                          .name("memoryOnlyCache_inCache")
90                          .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
91          ));
92          doAssertions(cacheManager.getCache("memoryOnlyCache_inCache"), ELEMENT_COUNT, 0);
93      }
94  
95      @Test
96      public void testOverflowToDisk() throws Exception {
97          cacheManager.addCache(new Cache(
98                  new CacheConfiguration()
99                          .maxElementsInMemory(10)
100                         .maxElementsOnDisk(20)
101                         .overflowToDisk(true)
102                         .statistics(true)
103                         .name("overflowToDiskCache_onHeap")
104                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
105         ));
106         doAssertions(cacheManager.getCache("overflowToDiskCache_onHeap"), ELEMENT_COUNT, 0);
107 
108         cacheManager.addCache(new Cache(
109                 new CacheConfiguration()
110                         .maxElementsInMemory(10)
111                         .maxElementsOnDisk(20)
112                         .overflowToDisk(true)
113                         .statistics(true)
114                         .name("overflowToDiskCache_inMemory")
115                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
116         ));
117         doAssertions(cacheManager.getCache("overflowToDiskCache_inMemory"), ELEMENT_COUNT, 0);
118 
119         cacheManager.addCache(new Cache(
120                 new CacheConfiguration()
121                         .maxElementsInMemory(10)
122                         .maxElementsOnDisk(20)
123                         .overflowToDisk(true)
124                         .statistics(true)
125                         .name("overflowToDiskCache_inCache")
126                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
127         ));
128         doAssertions(cacheManager.getCache("overflowToDiskCache_inCache"), 10, ELEMENT_COUNT - 10);
129     }
130 
131     @Test
132     public void testDiskPersistent() throws Exception {
133         cacheManager.addCache(new Cache(
134                 new CacheConfiguration()
135                         .maxElementsInMemory(10)
136                         .maxElementsOnDisk(20)
137                         .overflowToDisk(true)
138                         .diskPersistent(true)
139                         .statistics(true)
140                         .name("diskPersistentCache_onHeap")
141                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALHEAP))
142         ));
143         doAssertions(cacheManager.getCache("diskPersistentCache_onHeap"), ELEMENT_COUNT, 0);
144 
145         cacheManager.addCache(new Cache(
146                 new CacheConfiguration()
147                         .maxElementsInMemory(10)
148                         .maxElementsOnDisk(20)
149                         .overflowToDisk(true)
150                         .diskPersistent(true)
151                         .statistics(true)
152                         .name("diskPersistentCache_inMemory")
153                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.LOCALMEMORY))
154         ));
155         doAssertions(cacheManager.getCache("diskPersistentCache_inMemory"), ELEMENT_COUNT, 0);
156 
157         cacheManager.addCache(new Cache(
158                 new CacheConfiguration()
159                         .maxElementsInMemory(10)
160                         .maxElementsOnDisk(20)
161                         .overflowToDisk(true)
162                         .diskPersistent(true)
163                         .statistics(true)
164                         .name("diskPersistentCache_inCache")
165                         .pinning(new PinningConfiguration().store(PinningConfiguration.Store.INCACHE))
166         ));
167         doAssertions(cacheManager.getCache("diskPersistentCache_inCache"), 10, ELEMENT_COUNT - 10);
168     }
169 
170     private void doAssertions(Cache cache, long expectedMemoryHits, long expectedDiskHits) {
171         for (int i = 0; i < ELEMENT_COUNT; i++) {
172             cache.put(new Element(i, i));
173         }
174 
175         Assert.assertEquals(ELEMENT_COUNT, cache.getSize());
176 
177         for (int i = 0; i < ELEMENT_COUNT; i++) {
178             assertNotNull(cache.get(i));
179         }
180 
181         Assert.assertEquals(expectedMemoryHits, cache.getStatistics().getInMemoryHits());
182         Assert.assertEquals(ELEMENT_COUNT - expectedMemoryHits, cache.getStatistics().getInMemoryMisses());
183         Assert.assertEquals(expectedDiskHits, cache.getStatistics().getOnDiskHits());
184         Assert.assertEquals(0, cache.getStatistics().getOnDiskMisses());
185         Assert.assertEquals(ELEMENT_COUNT - expectedMemoryHits, cache.getStatistics().getEvictionCount());
186     }
187 
188 }
189