61271c8c87bbcae81fefcef9199d01b7542c5968.svn-base 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. *
  3. */
  4. package com.yihu.util.mapRedis;
  5. import java.util.ArrayList;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import net.sf.json.JSONObject;
  11. /**
  12. * 使用Map实现redis的功能
  13. * @author lch
  14. *
  15. */
  16. public class MapForRedisUtils implements Runnable,IRedisUtils {
  17. private static MapForRedisUtils mapForRedisUtils=null;
  18. private static Map<String,String> map=new ConcurrentHashMap<String,String>();
  19. private static Map<String,Long> expireMap=new ConcurrentHashMap<String,Long>();
  20. /**
  21. * 存储值
  22. * @param key 健
  23. * @param val 值
  24. * @param minute 过期时间(分钟)
  25. */
  26. public void setValue(String key,String val,int minute){
  27. map.put(key,val);
  28. expireMap.put(key,System.currentTimeMillis()+minute*60L*1000);
  29. }
  30. /**
  31. * 获取值
  32. * @param key 健
  33. */
  34. public String getValue(String key){
  35. try{
  36. return map.get(key);
  37. }catch(Exception e){
  38. e.printStackTrace();
  39. return null;
  40. }
  41. }
  42. /**
  43. * 获取过期时间(相对于1970-1-1的毫秒数)
  44. * @param key 健
  45. */
  46. public long getExpireTime(String key){
  47. try{
  48. return expireMap.get(key);
  49. }catch(Exception e){
  50. e.printStackTrace();
  51. return 0;
  52. }
  53. }
  54. /**
  55. * 设置过期时间(毫秒数)
  56. * @param key 健
  57. * @param expireMills 多少毫秒
  58. */
  59. public long setExpireTime(String key,int minute){
  60. try{
  61. return expireMap.put(key,System.currentTimeMillis()+minute*60L*1000);
  62. }catch(Exception e){
  63. e.printStackTrace();
  64. return 0;
  65. }
  66. }
  67. @Override
  68. public void run() {
  69. while(true){
  70. try{
  71. //移除过期的数据
  72. List<String> needRemoveKey=new ArrayList<String>();
  73. for (Map.Entry<String, Long> entry : MapForRedisUtils.expireMap.entrySet()) {
  74. if(entry.getValue()!=null&&entry.getValue()<=System.currentTimeMillis()){
  75. needRemoveKey.add(entry.getKey());
  76. }
  77. }
  78. for(String key:needRemoveKey){
  79. map.remove(key);
  80. expireMap.remove(key);
  81. }
  82. try {
  83. Thread.sleep(10000);
  84. } catch (Exception e1) {
  85. e1.printStackTrace();
  86. }
  87. }catch(Exception e){
  88. try {
  89. Thread.sleep(1000);
  90. } catch (Exception e1) {
  91. e1.printStackTrace();
  92. }
  93. }
  94. }
  95. }
  96. public synchronized IRedisUtils init(){
  97. if(mapForRedisUtils==null){
  98. mapForRedisUtils=new MapForRedisUtils();
  99. Thread t1=new Thread(mapForRedisUtils);
  100. t1.start();
  101. }
  102. return mapForRedisUtils;
  103. }
  104. public static void main(String[] args) throws InterruptedException {
  105. IRedisUtils mapForRedisUtils=new MapForRedisUtils();
  106. mapForRedisUtils.init();//全局只要允许一次就好
  107. mapForRedisUtils.setValue("test", "1111111", 1);
  108. System.out.println(mapForRedisUtils.getValue("test"));
  109. Thread.sleep(2*60*1000);
  110. System.out.println("2 min after,val="+mapForRedisUtils.getValue("test"));
  111. }
  112. }