缓存操作接口

  1. /**
  2. * 缓存操作接口
  3. *
  4. * @author xiudong
  5. *
  6. * @param <T>
  7. */
  8. public interface Cache<T> {
  9. /**
  10. * 刷新缓存数据
  11. *
  12. * @param key 缓存key
  13. * @param target 新数据
  14. */
  15. void refresh(String key, T target);
  16. /**
  17. * 获取缓存
  18. *
  19. * @param key 缓存key
  20. * @return 缓存数据
  21. */
  22. T getCache(String key);
  23. /**
  24. * 判断缓存是否过期
  25. *
  26. * @param key 缓存key
  27. * @return 如果缓存过期返回true, 否则返回false
  28. */
  29. Boolean isExpired(String key);
  30. /**
  31. * 设置缓存过期时间
  32. *
  33. * @param key 缓存key
  34. * @param millsec 缓存过期时间,单位:毫秒
  35. */
  36. void setExpired(Long millsec);
  37. /**
  38. * 是否存在缓存对象
  39. *
  40. * @param key 缓存key
  41. * @return 存在返回true,不存在返回false
  42. */
  43. Boolean exist(String key);
  44. }
  1. import java.util.Date;
  2. /**
  3. * 缓存实体
  4. *
  5. * @author xiudong
  6. *
  7. * @param <T>
  8. */
  9. public class LastCache<T> {
  10. /**
  11. * 上次缓存的数据
  12. */
  13. private T data;
  14. /**
  15. * 最后刷新时间
  16. */
  17. private long refreshtime;
  18. public LastCache(T data) {
  19. this(data, new Date().getTime());
  20. }
  21. public LastCache(T data, long refreshtime) {
  22. this.data = data;
  23. this.refreshtime = refreshtime;
  24. }
  25. public T getData() {
  26. return data;
  27. }
  28. public void setData(T data) {
  29. this.data = data;
  30. }
  31. public long getRefreshtime() {
  32. return refreshtime;
  33. }
  34. public void setRefreshtime(long refreshtime) {
  35. this.refreshtime = refreshtime;
  36. }
  37. }
    1. import java.util.Date;
    2. import java.util.Map;
    3. import java.util.concurrent.ConcurrentHashMap;
    4. /**
    5. * 简单的缓存模型
    6. *
    7. * @author xiudong
    8. *
    9. * @param <T>
    10. */
    11. public class SimpleCached<T> implements Cache<T> {
    12. /**
    13. * 缓存数据索引
    14. */
    15. private Map<String, LastCache<T>> cache = new ConcurrentHashMap<String, LastCache<T>>();
    16. /**
    17. * 缓存超时时间,单位:毫秒
    18. */
    19. private Long expired = 0L;
    20. public SimpleCached() {
    21. this(5 * 1000 * 60L);
    22. }
    23. public SimpleCached(Long expired) {
    24. this.expired = expired;
    25. }
    26. @Override
    27. public void refresh(String key, T target) {
    28. if (cache.containsKey(key)) {
    29. cache.remove(key);
    30. }
    31. cache.put(key, new LastCache<T>(target));
    32. }
    33. @Override
    34. public T getCache(String key) {
    35. if (!this.exist(key)) {
    36. return null;
    37. }
    38. return cache.get(key).getData();
    39. }
    40. @Override
    41. public Boolean isExpired(String key) {
    42. if (!this.exist(key)) {
    43. return null;
    44. }
    45. long currtime = new Date().getTime();
    46. long lasttime = cache.get(key).getRefreshtime();
    47. return (currtime - lasttime) > expired;
    48. }
    49. @Override
    50. public void setExpired(Long millsec) {
    51. this.expired = millsec;
    52. }
    53. @Override
    54. public Boolean exist(String key) {
    55. return cache.containsKey(key);
    56. }
    57. }

写了一个Java的简单缓存模型的更多相关文章

  1. 分享:写了一个 java 调用 C语言 开发的动态库的范例

    分享:写了一个 java 调用 C语言 开发的动态库的范例 cfunction.h   代码#pragma once#ifdef __cplusplusextern "C" {#e ...

  2. 我写了一个java实体类,implements了Serializable接口,然后我如何让serialversionUID自动生成

    写了一个java实体类,implements了Serializable接口,让serialversionUID自动生成方法: 1.点击类旁边的警告符号: 2.选择Add generated seria ...

  3. 师兄写的一个JAVA播放器的源代码(转)

    师兄写的一个JAVA播放器的源代码 MediaPlayer.java------------------------------------------------------------------ ...

  4. 自己写的一个 java swing 的闹钟

    上一周新入职,把代码down下来之后,领导和我讲了一些大概的业务以及代码流程(领导是技术出身),领导让我自己先看看代码,然后我看了两天,觉得已经完全可以接任务了,但是领导却迟迟没有分配任务给我,虽然他 ...

  5. 第一次尝试学习java 安装jdk 与配置环境变量 写第一个java程序 并运行

    第一次学习java,今天知道了java之父叫  詹姆斯.高司令 其它的记不住太多,首先我们先来安装jdk 百度搜索jdk12   (现在的jdk为12版本)安装稳定版 找到javaSE12X..  下 ...

  6. 使用interllij IDEA 写第一个Java程序

    安装interllij IDEA interllij IDEA简称IDEA,是最好用的Java集成开发环境.你只需要安装一个IDEA,就可以立马开始学习Java,不用再费心去配置Java环境. IDE ...

  7. 001. 使用IDEA新建一个JAVA最简单的Spring MVC JAVAWEB程序

    1. 我们打开一个空的IDEA 2. 选择Java之后点击Next 3. 点击Next创建空白工程 4. 给工程取个名字,叫MYIDEA 5. 勾选之后,点击This Window按钮 6. 我们可以 ...

  8. [Android面试题-7] 写出一个Java的Singleton类(即单例类)

    1.首先明确单例的概念和特点: a>单例类只能有一个实例 b>单例类必须自己创建一个自己的唯一实例 c>单例类必须为其他所有对象提供这个实例 2.单例具有几种模式,最简单的两种分别是 ...

  9. 用intellij idea 写第一个Java程序

    Java小白,还不怎么会eclipse,只会在命令行用javac编译并java运行编译后的类. 英文还不好orz 发现创建项目后,能build但就是不能run... 找了半天教程没找着,去官网溜了一下 ...

随机推荐

  1. Luogu P1010 幂次方

    [橙题不会做系列]QAQ 是李老师上课的题目-- 这题最开始想法是打表做.事实证明这样做也可以( 老师用的是位运算-- 这种一步步分解也能想到用递归qwq #include <algorithm ...

  2. Python 互斥锁

    互斥锁Mutex的使用 多个线程处理共享数据,数据会出现问题: 2.7之前每100指令切换一次GIL锁,线程就会sleep,线程会把前100条处理指令存放在CPU缓存内,切换GIL锁后放入另外一个线程 ...

  3. @Transacitonal注解不生效之spring中expose-proxy的作用与原理

    几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的. 如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢? 答案就是在<aop:aspect ...

  4. 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 ...

  5. HDU 4283 You Are the One ——区间dp

    参考了许多大佬  尤其是https://blog.csdn.net/woshi250hua/article/details/7973824这一篇 ,最后我再加一点我的见解. 大意是 给定一个序列,序列 ...

  6. 如何使用JavaScript UI控件(WijmoJS)构建Electron应用程序

    概述 What is Electron? Electron是一个使用JavaScript.HTML和CSS构建跨平台桌面应用程序的框架. 您可以将Electron与纯JavaScript或您选择的Ja ...

  7. Lintcode481-Binary Tree Leaf Sum-Easy

    481. Binary Tree Leaf Sum Given a binary tree, calculate the sum of leaves. Example Example 1: Input ...

  8. Rancher 容器管理平台-免费视频培训-链接及内容-第三季

    Rancher 容器管理平台-免费视频培训-链接及内容 第三季 第5期-2018年05月10日-持续集成的容器化实践回放网址:http://www.itdks.com/liveevent/detail ...

  9. Spring框架基础

    1         Spring框架 1.1           Spring的基本概念 是一个轻量级的框架,提供基础的开发包,包括消息.web通讯.数据库.大数据.授权.手机应用.session管理 ...

  10. Angular 学习笔记 (Material Datepicker)

    https://material.angular.io/components/datepicker/overview 官网介绍很清楚了,这里记入一下我比较不熟悉的. 1. moment js Angu ...