1 package org.apache.commons.jcs3.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 java.util.Iterator;
23 import java.util.concurrent.CopyOnWriteArraySet;
24
25 import org.apache.commons.jcs3.log.Log;
26 import org.apache.commons.jcs3.log.LogManager;
27 import org.apache.commons.jcs3.utils.timing.ElapsedTimer;
28
29
30
31
32
33 public class ShrinkerThread
34 implements Runnable
35 {
36
37 private static final Log log = LogManager.getLog( ShrinkerThread.class );
38
39
40 private final CopyOnWriteArraySet<JDBCDiskCache<?, ?>> shrinkSet =
41 new CopyOnWriteArraySet<>();
42
43
44 private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000;
45
46
47
48
49
50
51 private long pauseBetweenRegionCallsMillis = DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS;
52
53
54
55
56 protected ShrinkerThread()
57 {
58 }
59
60
61
62
63
64
65 public void addDiskCacheToShrinkList( final JDBCDiskCache<?, ?> diskCache )
66 {
67
68
69
70
71 shrinkSet.add( diskCache );
72 }
73
74
75
76
77 @Override
78 public void run()
79 {
80 try
81 {
82 deleteExpiredFromAllRegisteredRegions();
83 }
84 catch ( final Throwable e )
85 {
86 log.error( "Caught an exception while trying to delete expired items.", e );
87 }
88 }
89
90
91
92
93 private void deleteExpiredFromAllRegisteredRegions()
94 {
95 log.info( "Running JDBC disk cache shrinker. Number of regions [{0}]",
96 shrinkSet::size);
97
98 for (final Iterator<JDBCDiskCache<?, ?>> i = shrinkSet.iterator(); i.hasNext();)
99 {
100 final JDBCDiskCache<?, ?> cache = i.next();
101 final ElapsedTimer timer = new ElapsedTimer();
102 final int deleted = cache.deleteExpired();
103
104 log.info( "Deleted [{0}] expired for region [{1}] for table [{2}] in {3} ms.",
105 deleted, cache.getCacheName(), cache.getTableName(), timer.getElapsedTime() );
106
107
108 if ( i.hasNext() )
109 {
110 log.info( "Pausing for [{0}] ms before shrinking the next region.",
111 this.getPauseBetweenRegionCallsMillis() );
112
113 try
114 {
115 Thread.sleep( this.getPauseBetweenRegionCallsMillis() );
116 }
117 catch ( final InterruptedException e )
118 {
119 log.warn( "Interrupted while waiting to delete expired for the next region." );
120 }
121 }
122 }
123 }
124
125
126
127
128
129
130
131 public void setPauseBetweenRegionCallsMillis( final long pauseBetweenRegionCallsMillis )
132 {
133 this.pauseBetweenRegionCallsMillis = pauseBetweenRegionCallsMillis;
134 }
135
136
137
138
139
140
141
142 public long getPauseBetweenRegionCallsMillis()
143 {
144 return pauseBetweenRegionCallsMillis;
145 }
146 }