转载:http://oyhk.iteye.com/blog/907712
说明:转载只是给自己一个笔记而已,没有什么别的商业用途。
java cache 简单应用
HK学习笔记
首先什么是java 缓存的应用大家就看这个东西吧!!! 我上传一个PPT大家可以下载!
其实java cache就是一个static map;当你把它初始化了,你可以在同一个线程里共用
下面我来做一个例子,这些都是初学者的哦.高手就不要介意了
- 如果 大家有什么不明白..请留言吧,我马上解释!!!
- 注意:想取到缓存的时候,一定要在同一个进程里,才能访问,不然的话是null的!
- 到于怎么才是同一个进程,就是测试类里main方法开始执行到毕业里的的线程,
- 或者子线程都可以访问共享的缓存
首先是一个类
1.首先定义一个static HashMap ; 这个map用来存放缓存的!!
- package com.oywk.cache;
- import java.util.Date;
- import java.util.HashMap;
- /**
- * @author HK
- *
- * @date 2011-2-15 下午09:40:00
- */
- public class CacheManager {
- private static HashMap cacheMap = new HashMap();
- /**
- * This class is singleton so private constructor is used.
- */
- private CacheManager() {
- super();
- }
- /**
- * returns cache item from hashmap
- * @param key
- * @return Cache
- */
- private synchronized static Cache getCache(String key) {
- return (Cache)cacheMap.get(key);
- }
- /**
- * Looks at the hashmap if a cache item exists or not
- * @param key
- * @return Cache
- */
- private synchronized static boolean hasCache(String key) {
- return cacheMap.containsKey(key);
- }
- /**
- * Invalidates all cache
- */
- public synchronized static void invalidateAll() {
- cacheMap.clear();
- }
- /**
- * Invalidates a single cache item
- * @param key
- */
- public synchronized static void invalidate(String key) {
- cacheMap.remove(key);
- }
- /**
- * Adds new item to cache hashmap
- * @param key
- * @return Cache
- */
- private synchronized static void putCache(String key, Cache object) {
- cacheMap.put(key, object);
- }
- /**
- * Reads a cache item's content
- * @param key
- * @return
- */
- public static Cache getContent(String key) {
- if (hasCache(key)) {
- Cache cache = getCache(key);
- if (cacheExpired(cache)) {
- cache.setExpired(true);
- }
- return cache;
- } else {
- return null;
- }
- }
- /**
- *
- * @param key
- * @param content
- * @param ttl
- */
- public static void putContent(String key, Object content, long ttl) {
- Cache cache = new Cache();
- cache.setKey(key);
- cache.setValue(content);
- cache.setTimeOut(ttl + new Date().getTime());
- cache.setExpired(false);
- putCache(key, cache);
- }
- /** @modelguid {172828D6-3AB2-46C4-96E2-E72B34264031} */
- private static boolean cacheExpired(Cache cache) {
- if (cache == null) {
- return false;
- }
- long milisNow = new Date().getTime();
- long milisExpire = cache.getTimeOut();
- if (milisExpire < 0) { // Cache never expires
- return false;
- } else if (milisNow >= milisExpire) {
- return true;
- } else {
- return false;
- }
- }
- }
跟着下面又一个类
- package com.oywk.cache;
- /**
- * @author HK
- *
- * @date 2011-2-15 下午06:45:57
- */
- public class Cache {
- private String key;
- private Object value;
- private long timeOut;
- private boolean expired;
- public Cache() {
- super();
- }
- public Cache(String key, String value, long timeOut, boolean expired) {
- this.key = key;
- this.value = value;
- this.timeOut = timeOut;
- this.expired = expired;
- }
- public String getKey() {
- return key;
- }
- public long getTimeOut() {
- return timeOut;
- }
- public Object getValue() {
- return value;
- }
- public void setKey(String string) {
- key = string;
- }
- public void setTimeOut(long l) {
- timeOut = l;
- }
- public void setValue(Object object) {
- value = object;
- }
- public boolean isExpired() {
- return expired;
- }
- public void setExpired(boolean b) {
- expired = b;
- }
- }
下面是测试的类
- package com.oywk.cache;
- /**
- * @author HK
- *
- * @date 2011-2-16 下午02:21:38
- */
- public class CacheTest {
- public static void main(String[] args) {
- //在缓存里加入三个学生
- for(int i = 0;i<3;i++){
- Student s = new Student();
- s.setId(i);
- s.setName("student"+i);
- //把一个student 加入缓存
- CacheManager.putContent(""+i, s, 300000);
- }
- //从缓存中把一个student 取出来
- Student student = (Student) CacheManager.getContent("1").getValue();
- System.out.println("id : " +student.getId()+ " name : "+ student.getName());
- }
- }