API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.utils.caching. Cache View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.jot.utils.caching;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import net.jot.logger.JOTLogger;
import net.jot.utils.JOTUtilities;

/**
 * Lazy caching base
 * Stores data in a Synchronized Hashmap(so it can store null)
 * @author thibautc
 */
public class Cache {

	public String name = JOTUtilities.getShortClassname(getClass());
	private Map cache = Collections.synchronizedMap(new HashMap());
	public int hits = 0;
	public Date flushTime = new Date();

	/**
	 * Gets an item from the cache (lazilly cached)
	 * Key should be an immutable (ie: String)
	 * Infos: will be passed to read, can be anyhting that help you creation the value in read()
	 * @param key
	 * @return
	 */
	public Object get(Object key, CacheProvider provider)
	{
		hits++;
		if (!cache.containsKey(key))
		{
			synchronized (this)
			{
				if (!cache.containsKey(key))
				{
					Object value = null;
					try
					{
						if (provider != null)
						{
							value = provider.readCacheItem(key);
						}
					} catch (Exception e)
					{
						JOTLogger.logException(this, "Cache[" + name + "]Failed reading entry for'" + key + "' with provider:" + provider.getClass().getName(), e);
					}
					cache.put(key, value);
					JOTLogger.debug(JOTLogger.CAT_MAIN,this,"Cache", "[" + name + "]Lazyly Cached : " + key + " -> " + value);
				}
			}
		}
		JOTLogger.debug(JOTLogger.CAT_MAIN,this,"Cache", "[" + name + "]Read: " + key + " -> " + cache.get(key));
		return cache.get(key);
	}

	public synchronized void flushCache()
	{
		JOTLogger.debug(JOTLogger.CAT_MAIN,this,"Cache", "[" + name + "]Flushing :" + cache.size() + " elements");
		cache.clear();
		flushTime = new Date();
		hits = 0;
	}

	/**
	 * Set a name for this cache to be used in the stats
	 * Defaults to the class name
	 * @param n
	 */
	public void setName(String n)
	{
		name = n;
	}

	/**
	 * Return a stat string about this cache
	 * @return
	 */
	public String getStats()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("[");
		sb.append(name);
		sb.append("] Hits=");
		sb.append(hits);
		sb.append(", Size=");
		sb.append(cache.size());
		sb.append(", Time=");
		String time = flushTime.toString();
		time = JOTUtilities.formatDate(flushTime,false);
		sb.append(time);
		return sb.toString();
	}

	public Set keys()
	{
		return cache.keySet();
	}

	public boolean containsKey(Object key)
	{
		return cache.containsKey(key);
	}

	/**
	 * Force a value (put())
	 * @param urlKey
	 * @param url
	 */
	public void setValue(Object key, Object value)
	{
		JOTLogger.debug(JOTLogger.CAT_MAIN,this,"Cache", "[" + name + "]Set: " + key + " -> " + value);
		cache.put(key, value);
	}
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar