Java实现redis缓存效果变量过期

快来打我* 2024-04-08 12:55 122阅读 0赞

目录

  • 1 实现
  • 2 测试
  • 3 接口

1 实现

redis中的Expire命令用于设置 key 的过期时间,key 过期后将不再可用。不过有些变量我们不需要设置到redis中,只要存到本地就可以了,也需要过期时间,也可以同样方法定义一个全局变量的map之后键是变量,值是时间每次都进行比较。

  1. public class ExpireModel<T> {
  2. /**
  3. * 值
  4. *
  5. */
  6. private T value;
  7. /**
  8. * 值设置时间(时间戳,毫秒)
  9. *
  10. * @author ybwei
  11. */
  12. private Long setTime;
  13. /**
  14. * 过期时长(毫秒)
  15. */
  16. private Long expireTime;
  17. /**
  18. * @param value
  19. * @param expireTime
  20. */
  21. public ExpireModel(T value, Long expireTime) {
  22. super();
  23. this.value = value;
  24. this.expireTime = expireTime;
  25. this.setTime = System.currentTimeMillis();
  26. }
  27. /**
  28. * @Description:
  29. * @return
  30. */
  31. public T getValue() {
  32. T result = null;
  33. if (System.currentTimeMillis() - this.setTime <= this.expireTime) {
  34. result = this.value;
  35. }
  36. return result;
  37. }
  38. }

2 测试

  1. /**
  2. * @throws InterruptedException
  3. * @Description:
  4. */
  5. @Test
  6. public void testExpire() throws InterruptedException {
  7. // 失效时间1秒
  8. ExpireModel<String> em = new ExpireModel<>("aa", 1000L);
  9. log.info("value:{}", em.getValue());
  10. // 休眠时间2秒
  11. TimeUnit.SECONDS.sleep(2);
  12. log.info("value:{}", em.getValue());
  13. }

结果展示:

  1. [INFO ] 2022-08-27 11:32:46.680 [main] com.spring.pro.test.model.ExpireTest - value:aa
  2. [INFO ] 2022-08-27 11:32:48.692 [main] com.spring.pro.test.model.ExpireTest - value:null

3 接口

  1. import javax.annotation.PostConstruct;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import com.spring.pro.model.ExpireModel;
  5. @RestController
  6. public class IndexController {
  7. private ExpireModel<String> em = null;
  8. /**
  9. * 失效时间(2秒)
  10. *
  11. */
  12. private Long expireTime = 10 * 1000L;
  13. private int index = 0;
  14. /**
  15. * @Description:
  16. * @Author: ybwei
  17. * @Date: 2019年12月25日 上午11:14:58
  18. */
  19. @PostConstruct
  20. public void init() {
  21. em = new ExpireModel<String>("aa", expireTime);
  22. }
  23. /**
  24. * @Description:
  25. * @return
  26. */
  27. @GetMapping("/getValue")
  28. public String getValue() {
  29. String value = em.getValue();
  30. if (value == null) {
  31. value="bb" + index;
  32. em = new ExpireModel<String>(value, expireTime);
  33. index++;
  34. }
  35. return value;
  36. }
  37. }

4 使用HashMap方法实现

  1. import java.util.Date;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class CacheService {
  5. private Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
  6. /**
  7. * 设置变量值
  8. * @param key
  9. * @param ex 保存的时间毫秒
  10. * @param value 变量值
  11. */
  12. public void setex(String key, Long ex, String value){
  13. Map<String, Object> m = new HashMap<String,Object>();
  14. m.put("ex", ex); //剩余的时间毫秒
  15. m.put("extime", new Date().getTime() + ex); //具体过期的时间戳毫秒
  16. m.put("value", value);
  17. map.put(key, m);
  18. }
  19. public String get(String key){
  20. expor(key);
  21. if(map.containsKey(key)){
  22. Map<String, Object> obj = map.get(key);
  23. return String.valueOf(obj.get("value"));
  24. }
  25. return null;
  26. }
  27. /**
  28. * 计算剩余时间
  29. * @param key
  30. */
  31. private void expor(String key){
  32. if(map.containsKey(key)){
  33. Map<String, Object> obj = map.get(key);
  34. Long ex = (Long) obj.get("ex");
  35. Long extime = (Long) obj.get("extime");
  36. if(extime < new Date().getTime()){
  37. map.remove(key);
  38. }else{
  39. obj.put("ex", extime - new Date().getTime());
  40. map.put(key, obj);
  41. }
  42. }
  43. }
  44. /**
  45. * 获取变量剩余时间
  46. * @param key
  47. * @return
  48. */
  49. public Long ttl(String key){
  50. expor(key);
  51. if(map.containsKey(key)){
  52. return (Long) map.get(key).get("ex");
  53. }
  54. return null;
  55. }
  56. }

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读