1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.cache.eviction

File LRUEvictionConfiguration.java

 

Coverage histogram

../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

4
11
8
1
123
41
10
0.91
1.38
8
1.25

Classes

Class Line # Actions
LRUEvictionConfiguration 28 11 0% 10 15
0.347826134.8%
 

Contributing tests

This file is covered by 161 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.cache.eviction;
21   
22    /**
23    * An helper for {@link EntryEvictionConfiguration.Algorithm#NONE} cache algorithm. Evicts the least recently used entry
24    * when thresholds are hit.
25    *
26    * @version $Id: ffcbd72a3533764cefa62654dbe8eb3fbe470af5 $
27    */
 
28    public class LRUEvictionConfiguration extends EntryEvictionConfiguration
29    {
30    /**
31    * The key to access the maximum entries the cache can contain.
32    */
33    public static final String MAXENTRIES_ID = "maxentries";
34   
35    /**
36    * @see #getLifespan()
37    */
38    public static final String LIFESPAN_ID = "lifespan";
39   
40    /**
41    * Create a new EntryEvictionConfiguration based on LRU algorithm.
42    */
 
43  730 toggle public LRUEvictionConfiguration()
44    {
45  730 setAlgorithm(Algorithm.LRU);
46    }
47   
48    /**
49    * Create a new EntryEvictionConfiguration with given capacity, eviction will be based on LRU algorithm.
50    *
51    * @param maxEntries the maximum entries the cache can contain.
52    * @since 4.3M1
53    */
 
54  0 toggle public LRUEvictionConfiguration(int maxEntries)
55    {
56  0 this();
57   
58  0 setMaxEntries(maxEntries);
59    }
60   
61    /**
62    * @param maxEntries see {@link #getMaxEntries()}
63    */
 
64  725 toggle public void setMaxEntries(int maxEntries)
65    {
66  725 put(MAXENTRIES_ID, maxEntries);
67    }
68   
69    /**
70    * @return the maximum entries the cache can contain. When the cache reaches that any element, the defined eviction
71    * algorithm kicks in to remove existing cache entries.
72    */
 
73  0 toggle public int getMaxEntries()
74    {
75  0 Object obj = get(MAXENTRIES_ID);
76   
77  0 return obj == null ? 0 : (Integer) get(MAXENTRIES_ID);
78    }
79   
80    /**
81    * @param maxIdle see {@link #getMaxIdle()}
82    * @since 7.4M2
83    */
 
84  109 toggle public void setMaxIdle(int maxIdle)
85    {
86  109 setTimeToLive(maxIdle);
87    }
88   
89    /**
90    * @return the time a cache entry will continue to stay in the cache after being last accessed, in seconds. When
91    * the time is reached, the entry is expired and removed from the cache. In addition, when the cache
92    * reaches its maximum number of entries, the defined eviction algorithm is used (e.g. LRU) and thus an
93    * entry can stay less time in the cache than its maximum defined time.
94    * @since 7.4M2
95    */
 
96  0 toggle public int getMaxIdle()
97    {
98  0 return getTimeToLive();
99    }
100   
101    /**
102    * @param lifespan see {@link #getLifespan()}
103    * @since 7.4M2
104    */
 
105  7 toggle public void setLifespan(int lifespan)
106    {
107  7 put(LIFESPAN_ID, lifespan);
108    }
109   
110    /**
111    * @return the maximum lifespan of a cache entry, after which the entry is expired and removed from the cache, in
112    * seconds. In addition, when the cache reaches its maximum number of entries, the defined eviction
113    * algorithm is used (e.g. LRU) and thus an entry can stay less time in the cache than its maximum defined
114    * time.
115    * @since 7.4M2
116    */
 
117  0 toggle public int getLifespan()
118    {
119  0 Object obj = get(LIFESPAN_ID);
120   
121  0 return obj == null ? 0 : (Integer) get(LIFESPAN_ID);
122    }
123    }