cache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include "base/define.h"
  3. #include "base/error.h"
  4. #include "util/thread.h"
  5. #include <mutex>
  6. #include <list>
  7. namespace ylib
  8. {
  9. /*********************************************************************
  10. * class:缓存
  11. *********************************************************************/
  12. class cache:public ylib::error_base,private ylib::ithread
  13. {
  14. public:
  15. struct cache_info
  16. {
  17. cache_info()
  18. {
  19. update_sec = 0;
  20. timeout_sec = 0;
  21. update = false;
  22. }
  23. // 创建时间
  24. timestamp update_sec;
  25. // 过期时间
  26. uint32 timeout_sec;
  27. // 内容
  28. std::string value;
  29. // 更新
  30. bool update;
  31. };
  32. cache();
  33. ~cache();
  34. /***************************************************************************************************
  35. * function:启动
  36. * param
  37. * local_path : 缓存保存路径[默认=空],空则不保存本地。
  38. ****************************************************************************************************/
  39. bool start(const std::string& local_path);
  40. /***************************************************************************************************
  41. * function:停止
  42. ****************************************************************************************************/
  43. void stop();
  44. /***************************************************************************************************
  45. * function:读
  46. * param
  47. * name : 键
  48. * value : 值
  49. ****************************************************************************************************/
  50. bool read(const std::string& name,std::string& value);
  51. /***************************************************************************************************
  52. * function:写
  53. * param
  54. * name : 键
  55. * value : 值
  56. * timeout_sec : 过期时间 -2=不设置
  57. ****************************************************************************************************/
  58. bool write(const std::string& name, const std::string& value,int32 timeout_sec = -2);
  59. bool write(const std::string& name, const std::string& value,timestamp update_sec,int32 timeout_sec);
  60. /***************************************************************************************************
  61. * function:是否存在
  62. * param
  63. * name : 键
  64. ****************************************************************************************************/
  65. bool exist(const std::string& name);
  66. /***************************************************************************************************
  67. * function:失效时间
  68. * param
  69. * name : 键
  70. * return
  71. * >=0 则为失效时间,-1=永不过期 -2=无此键
  72. ****************************************************************************************************/
  73. int32 expire(const std::string& name);
  74. /***************************************************************************************************
  75. * function:删除
  76. * param
  77. * name : 键
  78. ****************************************************************************************************/
  79. bool remove(const std::string& name);
  80. /***************************************************************************************************
  81. * function:更新过期
  82. ****************************************************************************************************/
  83. bool update(const std::string& name);
  84. /***************************************************************************************************
  85. * function:清空
  86. ****************************************************************************************************/
  87. void clear();
  88. /***************************************************************************************************
  89. * function:取MAP
  90. ****************************************************************************************************/
  91. std::map<std::string,cache_info*> *list();
  92. std::mutex* mutex();
  93. private:
  94. bool _exist(const std::string& name,bool lock);
  95. // 通过 ithread 继承
  96. virtual bool run() override;
  97. private:
  98. // 本地缓存路径
  99. std::string m_local_path;
  100. // 读写锁
  101. std::mutex m_mutex;
  102. // 缓存数据
  103. std::map<std::string, cache_info*> m_cache;
  104. };
  105. }