Skip to content

Commit c3c4be3

Browse files
author
James Taylor
committed
- Removed creatTime from ICacheElement (not used, the one in
IElementAttributes is). - Made CompositeCache#get actually return null when an element is not found (duh =) - Expanded the detail page of the cache admin servlet to include expiration information. git-svn-id: https://svn.apache.org/repos/asf/jakarta/jcs/trunk@224001 13f79535-47bb-0310-9956-ffa450edef68
1 parent 274fd6f commit c3c4be3

File tree

6 files changed

+100
-44
lines changed

6 files changed

+100
-44
lines changed

src/java/org/apache/jcs/admin/servlet/JCSAdminServlet.java

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
import java.io.IOException;
44
import java.io.OutputStream;
55
import java.io.ObjectOutputStream;
6+
import java.io.Serializable;
67
import java.util.Arrays;
78
import java.util.LinkedList;
89
import java.util.Iterator;
910
import java.util.Map;
11+
import java.util.Date;
12+
import java.text.DateFormat;
1013
import javax.servlet.http.HttpServletRequest;
1114
import javax.servlet.http.HttpServletResponse;
1215

1316
import org.apache.jcs.engine.CacheConstants;
1417
import org.apache.jcs.engine.memory.MemoryCache;
1518
import org.apache.jcs.engine.behavior.ICache;
1619
import org.apache.jcs.engine.behavior.ICacheElement;
20+
import org.apache.jcs.engine.behavior.IElementAttributes;
1721
import org.apache.jcs.engine.control.CompositeCacheManager;
1822
import org.apache.jcs.engine.control.CompositeCache;
1923
import org.apache.velocity.Template;
@@ -137,7 +141,7 @@ else if ( action.equals( DETAIL_ACTION ) )
137141
if ( templateName == REGION_DETAIL_TEMPLATE_NAME )
138142
{
139143
context.put( "cacheName", cacheName );
140-
context.put( "keys", getSortedKeys( cacheName ) );
144+
context.put( "elementInfoRecords", buildElementInfo( cacheName ) );
141145
}
142146
else if ( templateName == DEFAULT_TEMPLATE_NAME )
143147
{
@@ -148,15 +152,50 @@ else if ( templateName == DEFAULT_TEMPLATE_NAME )
148152
}
149153
}
150154

151-
private Object[] getSortedKeys( String cacheName )
155+
private LinkedList buildElementInfo( String cacheName ) throws Exception
152156
{
153-
CompositeCache cache = ( CompositeCache ) cacheHub.getCache( cacheName );
157+
CompositeCache cache =
158+
( CompositeCache ) cacheHub.getCache( cacheName );
154159

155160
Object[] keys = cache.getMemoryCache().getKeyArray();
156161

157162
Arrays.sort( keys );
158163

159-
return keys;
164+
LinkedList records = new LinkedList();
165+
166+
ICacheElement element;
167+
IElementAttributes attributes;
168+
CacheElementInfo elementInfo;
169+
170+
DateFormat format = DateFormat.getDateTimeInstance( DateFormat.SHORT,
171+
DateFormat.SHORT );
172+
173+
long now = System.currentTimeMillis();
174+
175+
for ( int i = 0; i < keys.length; i++ )
176+
{
177+
element =
178+
cache.getMemoryCache().getQuiet( (Serializable) keys[ i ] );
179+
180+
attributes = element.getElementAttributes();
181+
182+
elementInfo = new CacheElementInfo();
183+
184+
elementInfo.key = String.valueOf( keys[ i ] );
185+
elementInfo.eternal = attributes.getIsEternal();
186+
elementInfo.maxLifeSeconds = attributes.getMaxLifeSeconds();
187+
188+
elementInfo.createTime =
189+
format.format( new Date( attributes.getCreateTime() ) );
190+
191+
elementInfo.expiresInSeconds =
192+
( now - attributes.getCreateTime()
193+
- ( attributes.getMaxLifeSeconds() * 1000 ) ) / -1000;
194+
195+
records.add( elementInfo );
196+
}
197+
198+
return records;
160199
}
161200

162201
private LinkedList buildCacheInfo() throws Exception
@@ -255,6 +294,41 @@ public String getStatus()
255294
}
256295
}
257296

297+
/** Stores info on a cache element for the template */
298+
public class CacheElementInfo
299+
{
300+
String key = null;
301+
boolean eternal = false;
302+
String createTime = null;
303+
long maxLifeSeconds = -1;
304+
long expiresInSeconds = -1;
305+
306+
public String getKey()
307+
{
308+
return key;
309+
}
310+
311+
public boolean isEternal()
312+
{
313+
return eternal;
314+
}
315+
316+
public String getCreateTime()
317+
{
318+
return createTime;
319+
}
320+
321+
public long getMaxLifeSeconds()
322+
{
323+
return maxLifeSeconds;
324+
}
325+
326+
public long getExpiresInSeconds()
327+
{
328+
return expiresInSeconds;
329+
}
330+
}
331+
258332
/**
259333
* Keeps track of the number of bytes written to it, but doesn't write them
260334
* anywhere.

src/java/org/apache/jcs/admin/servlet/JCSAdminServletRegionDetail.vm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99
<table border="1" cellpadding="5" >
1010
<tr>
1111
<th> Key </th>
12+
<th> Eternal? </th>
13+
<th> Create time </th>
14+
<th> Max Life (s) </th>
15+
<th> Till Expiration (s) </th>
1216
</tr>
1317

14-
#foreach ( $key in $keys )
18+
#foreach ( $element in $elementInfoRecords )
19+
1520
<tr>
16-
<td> $key </td>
21+
<td> $element.key </td>
22+
<td> $element.eternal </td>
23+
<td> $element.createTime </td>
24+
<td> $element.maxLifeSeconds </td>
25+
<td> $element.expiresInSeconds </td>
1726
<td> <a href="?action=remove&cacheName=${cacheName}&key=${key}"> Remove </a> </td>
1827
</tr>
1928
#end

src/java/org/apache/jcs/auxiliary/disk/PurgatoryElement.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,4 @@ public void setElementAttributes( IElementAttributes attr )
159159
{
160160
cacheElement.setElementAttributes( attr );
161161
}
162-
163-
/**
164-
* @see ICacheElement#getCreateTime
165-
*/
166-
public long getCreateTime()
167-
{
168-
return cacheElement.getCreateTime();
169-
}
170162
}

src/java/org/apache/jcs/engine/CacheElement.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public class CacheElement implements ICacheElement, Serializable
2222
public final Serializable key;
2323
/** Description of the Field */
2424
public final Serializable val;
25-
/** Description of the Field */
26-
public final long createTime;
2725

2826
/** Description of the Field */
2927
public ElementAttributes attr;
@@ -44,7 +42,6 @@ public CacheElement( String cacheName, Serializable key, Serializable val )
4442
this.cacheName = cacheName;
4543
this.key = key;
4644
this.val = val;
47-
createTime = System.currentTimeMillis();
4845
}
4946

5047

@@ -115,17 +112,6 @@ public IElementAttributes getElementAttributes()
115112
return this.attr;
116113
}
117114

118-
119-
/**
120-
* Gets the createTime attribute of the CacheElement object
121-
*
122-
* @return The createTime value
123-
*/
124-
public long getCreateTime()
125-
{
126-
return this.createTime;
127-
}
128-
129115
/** Description of the Method */
130116
public int hashCode()
131117
{

src/java/org/apache/jcs/engine/behavior/ICacheElement.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,4 @@ public interface ICacheElement extends Serializable
5353
* @param attr The new attributes value
5454
*/
5555
public void setElementAttributes( IElementAttributes attr );
56-
57-
58-
/**
59-
* Gets the createTime attribute of the ICacheElement object
60-
*
61-
* @return The createTime value
62-
*/
63-
public long getCreateTime();
64-
65-
// allow thread-safe operations.
66-
// public Object clone();
6756
}

src/java/org/apache/jcs/engine/control/CompositeCache.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,8 @@ protected ICacheElement get( Serializable key, boolean localOnly )
449449
{
450450
ICacheElement element = null;
451451

452+
boolean found = false;
453+
452454
if ( log.isDebugEnabled() )
453455
{
454456
log.debug( "get: key = " + key + ", localOnly = " + localOnly );
@@ -475,6 +477,8 @@ protected ICacheElement get( Serializable key, boolean localOnly )
475477
missCountExpired++;
476478

477479
remove( element );
480+
481+
element = null;
478482
}
479483
else
480484
{
@@ -487,6 +491,8 @@ protected ICacheElement get( Serializable key, boolean localOnly )
487491

488492
hitCountRam++;
489493
}
494+
495+
found = true;
490496
}
491497
else
492498
{
@@ -528,7 +534,6 @@ protected ICacheElement get( Serializable key, boolean localOnly )
528534

529535
if ( element != null )
530536
{
531-
532537
// Item found in one of the auxiliary caches.
533538

534539
if ( isExpired( element ) )
@@ -542,6 +547,8 @@ protected ICacheElement get( Serializable key, boolean localOnly )
542547
missCountExpired++;
543548

544549
remove( element );
550+
551+
element = null;
545552
}
546553
else
547554
{
@@ -561,6 +568,8 @@ protected ICacheElement get( Serializable key, boolean localOnly )
561568
memCache.update( element );
562569
}
563570

571+
found = true;
572+
564573
break;
565574
}
566575
}
@@ -573,16 +582,14 @@ protected ICacheElement get( Serializable key, boolean localOnly )
573582
log.error( e );
574583
}
575584

576-
if ( element == null )
585+
if ( ! found )
577586
{
578587
missCountNotFound++;
579588

580589
if ( log.isDebugEnabled() )
581590
{
582591
log.debug( cacheName + " - Miss" );
583592
}
584-
585-
return null;
586593
}
587594

588595
return element;
@@ -679,7 +686,6 @@ public boolean localRemove( Serializable key )
679686
*@param localOnly
680687
*@return
681688
*/
682-
683689
protected synchronized boolean remove( Serializable key,
684690
boolean localOnly )
685691
{

0 commit comments

Comments
 (0)