写了一个Java的简单缓存模型
缓存操作接口
- /**
- * 缓存操作接口
- *
- * @author xiudong
- *
- * @param <T>
- */
- public interface Cache<T> {
- /**
- * 刷新缓存数据
- *
- * @param key 缓存key
- * @param target 新数据
- */
- void refresh(String key, T target);
- /**
- * 获取缓存
- *
- * @param key 缓存key
- * @return 缓存数据
- */
- T getCache(String key);
- /**
- * 判断缓存是否过期
- *
- * @param key 缓存key
- * @return 如果缓存过期返回true, 否则返回false
- */
- Boolean isExpired(String key);
- /**
- * 设置缓存过期时间
- *
- * @param key 缓存key
- * @param millsec 缓存过期时间,单位:毫秒
- */
- void setExpired(Long millsec);
- /**
- * 是否存在缓存对象
- *
- * @param key 缓存key
- * @return 存在返回true,不存在返回false
- */
- Boolean exist(String key);
- }
- import java.util.Date;
- /**
- * 缓存实体
- *
- * @author xiudong
- *
- * @param <T>
- */
- public class LastCache<T> {
- /**
- * 上次缓存的数据
- */
- private T data;
- /**
- * 最后刷新时间
- */
- private long refreshtime;
- public LastCache(T data) {
- this(data, new Date().getTime());
- }
- public LastCache(T data, long refreshtime) {
- this.data = data;
- this.refreshtime = refreshtime;
- }
- public T getData() {
- return data;
- }
- public void setData(T data) {
- this.data = data;
- }
- public long getRefreshtime() {
- return refreshtime;
- }
- public void setRefreshtime(long refreshtime) {
- this.refreshtime = refreshtime;
- }
- }
- import java.util.Date;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- /**
- * 简单的缓存模型
- *
- * @author xiudong
- *
- * @param <T>
- */
- public class SimpleCached<T> implements Cache<T> {
- /**
- * 缓存数据索引
- */
- private Map<String, LastCache<T>> cache = new ConcurrentHashMap<String, LastCache<T>>();
- /**
- * 缓存超时时间,单位:毫秒
- */
- private Long expired = 0L;
- public SimpleCached() {
- this(5 * 1000 * 60L);
- }
- public SimpleCached(Long expired) {
- this.expired = expired;
- }
- @Override
- public void refresh(String key, T target) {
- if (cache.containsKey(key)) {
- cache.remove(key);
- }
- cache.put(key, new LastCache<T>(target));
- }
- @Override
- public T getCache(String key) {
- if (!this.exist(key)) {
- return null;
- }
- return cache.get(key).getData();
- }
- @Override
- public Boolean isExpired(String key) {
- if (!this.exist(key)) {
- return null;
- }
- long currtime = new Date().getTime();
- long lasttime = cache.get(key).getRefreshtime();
- return (currtime - lasttime) > expired;
- }
- @Override
- public void setExpired(Long millsec) {
- this.expired = millsec;
- }
- @Override
- public Boolean exist(String key) {
- return cache.containsKey(key);
- }
- }
写了一个Java的简单缓存模型的更多相关文章
- 分享:写了一个 java 调用 C语言 开发的动态库的范例
分享:写了一个 java 调用 C语言 开发的动态库的范例 cfunction.h 代码#pragma once#ifdef __cplusplusextern "C" {#e ...
- 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成
写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...
- 师兄写的一个JAVA播放器的源代码(转)
师兄写的一个JAVA播放器的源代码 MediaPlayer.java------------------------------------------------------------------ ...
- 自己写的一个 java swing 的闹钟
上一周新入职,把代码down下来之后,领导和我讲了一些大概的业务以及代码流程(领导是技术出身),领导让我自己先看看代码,然后我看了两天,觉得已经完全可以接任务了,但是领导却迟迟没有分配任务给我,虽然他 ...
- 第一次尝试学习java 安装jdk 与配置环境变量 写第一个java程序 并运行
第一次学习java,今天知道了java之父叫 詹姆斯.高司令 其它的记不住太多,首先我们先来安装jdk 百度搜索jdk12 (现在的jdk为12版本)安装稳定版 找到javaSE12X.. 下 ...
- 使用interllij IDEA 写第一个Java程序
安装interllij IDEA interllij IDEA简称IDEA,是最好用的Java集成开发环境.你只需要安装一个IDEA,就可以立马开始学习Java,不用再费心去配置Java环境. IDE ...
- 001. 使用IDEA新建一个JAVA最简单的Spring MVC JAVAWEB程序
1. 我们打开一个空的IDEA 2. 选择Java之后点击Next 3. 点击Next创建空白工程 4. 给工程取个名字,叫MYIDEA 5. 勾选之后,点击This Window按钮 6. 我们可以 ...
- [Android面试题-7] 写出一个Java的Singleton类(即单例类)
1.首先明确单例的概念和特点: a>单例类只能有一个实例 b>单例类必须自己创建一个自己的唯一实例 c>单例类必须为其他所有对象提供这个实例 2.单例具有几种模式,最简单的两种分别是 ...
- 用intellij idea 写第一个Java程序
Java小白,还不怎么会eclipse,只会在命令行用javac编译并java运行编译后的类. 英文还不好orz 发现创建项目后,能build但就是不能run... 找了半天教程没找着,去官网溜了一下 ...
随机推荐
- Luogu P1010 幂次方
[橙题不会做系列]QAQ 是李老师上课的题目-- 这题最开始想法是打表做.事实证明这样做也可以( 老师用的是位运算-- 这种一步步分解也能想到用递归qwq #include <algorithm ...
- Python 互斥锁
互斥锁Mutex的使用 多个线程处理共享数据,数据会出现问题: 2.7之前每100指令切换一次GIL锁,线程就会sleep,线程会把前100条处理指令存放在CPU缓存内,切换GIL锁后放入另外一个线程 ...
- @Transacitonal注解不生效之spring中expose-proxy的作用与原理
几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的. 如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢? 答案就是在<aop:aspect ...
- ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
centos7.5 使用into outfile备份失败 问题: mysql> select * from world.city into outfile '/tmp/world_city.da ...
- HDU 4283 You Are the One ——区间dp
参考了许多大佬 尤其是https://blog.csdn.net/woshi250hua/article/details/7973824这一篇 ,最后我再加一点我的见解. 大意是 给定一个序列,序列 ...
- 如何使用JavaScript UI控件(WijmoJS)构建Electron应用程序
概述 What is Electron? Electron是一个使用JavaScript.HTML和CSS构建跨平台桌面应用程序的框架. 您可以将Electron与纯JavaScript或您选择的Ja ...
- Lintcode481-Binary Tree Leaf Sum-Easy
481. Binary Tree Leaf Sum Given a binary tree, calculate the sum of leaves. Example Example 1: Input ...
- Rancher 容器管理平台-免费视频培训-链接及内容-第三季
Rancher 容器管理平台-免费视频培训-链接及内容 第三季 第5期-2018年05月10日-持续集成的容器化实践回放网址:http://www.itdks.com/liveevent/detail ...
- Spring框架基础
1 Spring框架 1.1 Spring的基本概念 是一个轻量级的框架,提供基础的开发包,包括消息.web通讯.数据库.大数据.授权.手机应用.session管理 ...
- Angular 学习笔记 (Material Datepicker)
https://material.angular.io/components/datepicker/overview 官网介绍很清楚了,这里记入一下我比较不熟悉的. 1. moment js Angu ...