package cache;
import java.util.HashMap;
import java.util.Map;
public class Cache1 {
private Map map=new HashMap();
private static Cache1 cache1=new Cache1();
private Cache1(){
}
public static Cache1 getInstanceCache1(){
return cache1;
}
public void setObject(String key,Object value){
map.put(key, value);
}
public Object getObject(String key){
Object value=null;
value=map.get(key);
if (value==null) {
value=getFromDB(key); // 从远程数据库获得
map.put(key, value);
}
return value;
}
/**
*仅仅只是模拟
*/
private Object getFromDB(String key) {
return null;
}
}
所谓的缓存,就是把经常用的数据存储到内存中,下次用的时候能很快的拿到。因而,上面的核心代码其实就是getObject。但是,得承认,上面的代码实在是太过简陋了。上面的测试程序很简单我就不写了。
package cache;
public interface GetResutl {
public Object get(Object o);
}
package cache;
public class ComputeSum implements GetResutl {
@Override
public Object get(Object o) {
if ( !(o instanceof Integer) ){
throw new IllegalArgumentException (o+"is not Integer");
}
int n=(int) o;
int result=0;
for (int i = 1; i <= n; i++) {
result+=i;
}
return result;
}
}
package cache;
public class ComputeMultiply implements GetResutl {
@Override
public Object get(Object o) {
if ( !(o instanceof Integer) ){
throw new IllegalArgumentException (o+"is not Integer");
}
int n=(int) o;
int result=1;
for (int i = 1; i <= n; i++) {
result*=i;
}
return result;
}
}
package cache;
import java.util.HashMap;
import java.util.Map;
public class Cache2 {
private Map map=new HashMap();
private GetResutl getResutl=null;
public Cache2(GetResutl r){
this.getResutl=r;
}
public void setObject(String key,Object value){
map.put(key, value);
}
public Object getObject(String key){
Object value=null;
value=map.get(key);
if (value==null) {
value=getResutl.get(key);
map.put(key, value);
}
return value;
}
}
package cache;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class ComputeSum implements GetResutl {
@Override
public Object get(Object o) {
final Long n=Long.valueOf((String) o);
Callable c=new Callable() {
@Override
public Long call(){
Long result=0L;
for (int i = 1; i <= n; i++) {
result+=i;
}
return result;
}
};
return new FutureTask<>(c);
}
}
package cache; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; public class Cache3 { private Map> map=new HashMap >(); private GetResutl getResutl=null; public Cache3(GetResutl r){ this.getResutl=r; } @SuppressWarnings("unchecked") public Object getObject(String key){ FutureTask
public static void main(String[] args) {
GetResutl getResutl=new ComputeSum();
Cache3 c3=new Cache3(getResutl);
long t1=0;
long t2=0;
t1=System.currentTimeMillis();
System.out.println(c3.getObject(""+1234566));
t2=System.currentTimeMillis();
System.out.println(t2-t1 );
t1=System.currentTimeMillis();
System.out.println(c3.getObject(""+1234566));
t2=System.currentTimeMillis();
System.out.println(t2-t1 );
t1=System.currentTimeMillis();
System.out.println(c3.getObject(""+1234567));
t2=System.currentTimeMillis();
System.out.println(t2-t1 );
t1=System.currentTimeMillis();
System.out.println(c3.getObject(""+1234567));
t2=System.currentTimeMillis();
System.out.println(t2-t1 );
t1=System.currentTimeMillis();
System.out.println(c3.getObject(""+1234567));
t2=System.currentTimeMillis();
System.out.println(t2-t1 );
}
输出:
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
interface Computable{
V compute(final K arg);
}
/**
* 实现简单缓存系统
* @author mzy
*
* @param key
* @param value
*/
public class FutureCache implements Computable{
private final ConcurrentHashMap> cache = new ConcurrentHashMap>();
private final Computable c;
public FutureCache(Computable c) {
this.c = c;
}
@Override
public V compute(final K key) {
while(true){
Future future = cache.get(key);
if(future == null){
Callable eval = new Callable() {
@Override
public V call() throws Exception { return c.compute(key); }
};
FutureTask ftask = new FutureTask(eval);
//使用putIfAbsent原子操作避免有上面if(future == null)引起的相同值的缺陷
future = cache.putIfAbsent(key, ftask);
if(future == null) { future = ftask; ftask.run(); }
}
try {
return future.get();
} catch (InterruptedException e) {
//出现中断异常应该从 cache中移除Future,防止缓存污染
cache.remove(key,future);
} catch (ExecutionException e) {
//执行中的异常应当抛出,获得恰当处理
throw new RuntimeException(e.getCause());
}
}
}
}
测试程序:
public class Test {
public static void main(String[] args) {
final Computable c = new Computable() {
@Override
public Integer compute(Integer arg) {
Integer sum = 0;
for(Integer i=0;i cache = new FutureCache(c);
long start = System.currentTimeMillis();
cache.compute(10000);
long stop = System.currentTimeMillis();
System.out.println(stop-start);
start = System.currentTimeMillis();
cache.compute(10000);
stop = System.currentTimeMillis();
System.out.println(stop-start);
start = System.currentTimeMillis();
cache.compute(10000);
stop = System.currentTimeMillis();
System.out.println(stop-start);
start = System.currentTimeMillis();
cache.compute(10000);
stop = System.currentTimeMillis();
System.out.println(stop-start);
}
}
http://my.oschina.net/ccdvote/blog/131876?p=1