1 package org.apache.commons.jcs.auxiliary.disk.jdbc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.Set;
28
29
30
31
32
33
34
35 public class ShrinkerThread
36 implements Runnable
37 {
38
39 private static final Log log = LogFactory.getLog( ShrinkerThread.class );
40
41
42 private final Set<JDBCDiskCache<?, ?>> shrinkSet =
43 Collections.synchronizedSet( new HashSet<JDBCDiskCache<?, ?>>() );
44
45
46 private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000;
47
48
49
50
51
52
53 private long pauseBetweenRegionCallsMillis = DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS;
54
55
56
57
58 protected ShrinkerThread()
59 {
60 super();
61 }
62
63
64
65
66
67
68 public void addDiskCacheToShrinkList( JDBCDiskCache<?, ?> diskCache )
69 {
70
71
72
73
74 shrinkSet.add( diskCache );
75 }
76
77
78
79
80 @Override
81 public void run()
82 {
83 try
84 {
85 deleteExpiredFromAllRegisteredRegions();
86 }
87 catch ( Throwable e )
88 {
89 log.error( "Caught an expcetion while trying to delete expired items.", e );
90 }
91 }
92
93
94
95
96 private void deleteExpiredFromAllRegisteredRegions()
97 {
98 if ( log.isInfoEnabled() )
99 {
100 log.info( "Running JDBC disk cache shrinker. Number of regions [" + shrinkSet.size() + "]" );
101 }
102
103 Object[] caches = null;
104
105 synchronized ( shrinkSet )
106 {
107 caches = this.shrinkSet.toArray();
108 }
109
110 if ( caches != null )
111 {
112 for ( int i = 0; i < caches.length; i++ )
113 {
114 JDBCDiskCache<?, ?> cache = (JDBCDiskCache<?, ?>) caches[i];
115
116 long start = System.currentTimeMillis();
117 int deleted = cache.deleteExpired();
118 long end = System.currentTimeMillis();
119
120 if ( log.isInfoEnabled() )
121 {
122 log.info( "Deleted [" + deleted + "] expired for region [" + cache.getCacheName() + "] for table ["
123 + cache.getTableName() + "] in " + ( end - start ) + " ms." );
124 }
125
126
127 if ( i < caches.length - 1 )
128 {
129 if ( log.isInfoEnabled() )
130 {
131 log.info( "Pausing for [" + this.getPauseBetweenRegionCallsMillis()
132 + "] ms. before shrinking the next region." );
133 }
134
135 try
136 {
137 Thread.sleep( this.getPauseBetweenRegionCallsMillis() );
138 }
139 catch ( InterruptedException e )
140 {
141 log.warn( "Interrupted while waiting to delete expired for the next region." );
142 }
143 }
144 }
145 }
146 }
147
148
149
150
151
152
153
154 public void setPauseBetweenRegionCallsMillis( long pauseBetweenRegionCallsMillis )
155 {
156 this.pauseBetweenRegionCallsMillis = pauseBetweenRegionCallsMillis;
157 }
158
159
160
161
162
163
164
165 public long getPauseBetweenRegionCallsMillis()
166 {
167 return pauseBetweenRegionCallsMillis;
168 }
169 }