java实现内存读取,性能优化,快速查找

 在我们开发系统的过程中往往会遇到数据读写的问题如果是经常访问的数据并且这些数据不经常变化。那么可以考虑把这批数据直接加载

到内存中以提高访问速度减少IO操作。这里从网上搜到一个例子非常典型,可以做为模板

有两个类:一个进行缓存的操作一个是实体类。

JAVA有两种开发模式 C/S,B/S。那么在开发B/S过程中可以再web服务器启动的过程中直接加载内存数据;C/S也一样,在初始化时就可以启动。


一下是两个类的代码:


package main;
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 main;


import java.util.Date;
import java.util.HashMap;


public class CacheManager {
        private static HashMap cacheMap = new HashMap();


        /**
         * This class is singleton so private constructor is used.
         */
        public 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
         */
        public synchronized static void putCache(String key, Cache cache) {
           cacheMap.put(key, cache);
        }


        /**
         * 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;
                }
        }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值