使用内存实现类似Redis缓存的内存缓存功能

Redis缓存作为常见的缓存应用,分布广泛,但有时候我们的系统根本不需要使用Redis或者其他第三方缓存软件,因为系统功能比较简单,尤其在缓存内容这块。所以,该文章将向你介绍,如何使用简单的系统内存作为缓存工具,替代Redis实现缓存功能。

主要使用ConcurrentHashMapScheduledExecutorService类,Java类中导入依赖语句:

import java.util.concurrent.*;

Service层写法

接口类:

public interface SimpleMemoryCacheService<K, V> {
    V get(K key);
    void put(K key, V value, long expireAfterAccessMillis);
    void remove(K key);
    void shutdown();
}

实现类:

@Service
public class SimpleMemoryCacheServiceImpl<K, V> implements SimpleMemoryCacheService<K, V> {
    private final ConcurrentHashMap<K, CacheObject<V>> cache = new ConcurrentHashMap<>();
    private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    public V get(K key) {
        CacheObject<V> cacheObject = cache.get(key);
        if (cacheObject != null) {
            cacheObject.setLastAccessTime(System.currentTimeMillis());
            return cacheObject.getValue();
        }
        return null;
    }

    public void put(K key, V value, long expireAfterAccessMillis) {
        CacheObject<V> cacheObject = new CacheObject<>(value, System.currentTimeMillis(), expireAfterAccessMillis);
        cache.put(key, cacheObject);
        executorService.schedule(() -> cleanup(key), expireAfterAccessMillis, TimeUnit.MILLISECONDS);
    }

    public void remove(K key) {
        cache.remove(key);
    }

    private void cleanup(K key) {
        CacheObject<V> cacheObject = cache.get(key);
        if (cacheObject != null && System.currentTimeMillis() - cacheObject.getLastAccessTime() > cacheObject.getExpireAfterAccessMillis()) {
            cache.remove(key);
        }
    }

    public void shutdown() {
        executorService.shutdown();
    }

    private static class CacheObject<V> {
        private V value;
        private long lastAccessTime;
        private long expireAfterAccessMillis;

        CacheObject(V value, long lastAccessTime, long expireAfterAccessMillis) {
            this.value = value;
            this.lastAccessTime = lastAccessTime;
            this.expireAfterAccessMillis = expireAfterAccessMillis;
        }

        public V getValue() {
            return value;
        }

        public long getLastAccessTime() {
            return lastAccessTime;
        }

        public void setLastAccessTime(long lastAccessTime) {
            this.lastAccessTime = lastAccessTime;
        }

        public long getExpireAfterAccessMillis() {
            return expireAfterAccessMillis;
        }
    }
}

至此,基本完成,下面简单说明使用方法。

在你的控制类或者其他业务层服务类中注入:

    @Autowired
    private SimpleMemoryCacheService simpleMemoryCacheService;

在你的代码中使用:

//往内存里添加缓存  参数:key,value,过期时间(值为Long.MAX_VALUE时永不过期)
simpleMemoryCacheService.put("nameXXX", "张三", Long.MAX_VALUE);

//往内存里添加缓存  参数:key,value,过期时间(五分钟后过期)
simpleMemoryCacheService.put("yzm", "XF168A", 300000);

//获取缓存内容  参数:key
Object obj = simpleMemoryCacheService.get("yzm");

//移除内存中某个缓存内容  参数:key
simpleMemoryCacheService.remove("nameXXX");
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值