AppCache.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "servant/AppCache.h"
  17. #include "servant/Communicator.h"
  18. namespace tars
  19. {
  20. //////////////////////////////////////////////////////////////////////
  21. // 缓存
  22. void AppCache::setCacheInfo(const string &sFile,int32_t iSynInterval)
  23. {
  24. try
  25. {
  26. TC_LockT<TC_ThreadMutex> lock(*this);
  27. string sPath = TC_File::extractFilePath(sFile);
  28. TC_File::makeDirRecursive(sPath);
  29. _file = sFile;
  30. _synInterval = iSynInterval;
  31. if (TC_File::isFileExistEx(_file))
  32. {
  33. _fileCache.parseFile(_file);
  34. }
  35. //如果是旧版本数据(无版本号)直接清理
  36. if (_fileCache.get(string(APPCACHE_ROOT_PATH)+"<tarsversion>","") == "")
  37. {
  38. TC_Config tFileCache;
  39. _fileCache = tFileCache;
  40. }
  41. }
  42. catch(exception &e)
  43. {
  44. TC_File::removeFile(_file, false);
  45. TLOGERROR("[TARS][AppCache setCacheInfo ex:" << e.what() << "]" << endl);
  46. }
  47. }
  48. string AppCache::get(const string & sName, const string sDomain)
  49. {
  50. if(_file.empty())
  51. {
  52. return "";
  53. }
  54. try
  55. {
  56. TC_LockT<TC_ThreadMutex> lock(*this);
  57. string sValue = _fileCache.get(string(APPCACHE_ROOT_PATH) + "/" + sDomain + "<" + sName + ">");
  58. return sValue;
  59. }
  60. catch(exception &e)
  61. {
  62. TLOGERROR("[TARS][AppCache get sName:" << sName << ",ex:" << e.what() << "]" << endl);
  63. }
  64. return "";
  65. }
  66. map<string, string> AppCache::getDomainMap(const string &path)
  67. {
  68. map<string, string> m;
  69. if(_file.empty())
  70. {
  71. return m;
  72. }
  73. try
  74. {
  75. TC_LockT<TC_ThreadMutex> lock(*this);
  76. m = _fileCache.getDomainMap(string(APPCACHE_ROOT_PATH) + "/" + path);
  77. }
  78. catch(exception &e)
  79. {
  80. TLOGERROR("[TARS][AppCache getDomainMap path:" << path << ",ex:" << e.what() << "]" << endl);
  81. }
  82. return m;
  83. }
  84. int AppCache::set(const string &sName,const string &sValue,const string sDomain)
  85. {
  86. if(_file.empty())
  87. {
  88. return -1;
  89. }
  90. try
  91. {
  92. TC_LockT<TC_ThreadMutex> lock(*this);
  93. map<string, string> m;
  94. m[sName] = sValue;
  95. TC_Config tConf;
  96. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH)+"/"+sDomain,m,true);
  97. if(_lastSynTime == 0) //第一次写数据 打印tarsversion
  98. {
  99. m.clear();
  100. m["tarsversion"] = _communicator->clientConfig().TarsVersion;
  101. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
  102. }
  103. {
  104. m.clear();
  105. m["modify"] = TC_Common::now2str("%Y-%m-%d %H:%M:%S");
  106. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
  107. }
  108. _fileCache.joinConfig(tConf,true);
  109. time_t now = TNOW;
  110. if(_lastSynTime + _synInterval/1000 > now)
  111. {
  112. return 0;
  113. }
  114. _lastSynTime = now;
  115. TC_File::save2file(_file,_fileCache.tostr());
  116. return 0;
  117. }
  118. catch(exception &e)
  119. {
  120. TLOGERROR("[TARS][AppCache set name:" << sName << ",value:" << sValue << ",ex:" << e.what() << "]" << endl);
  121. }
  122. return -1;
  123. }
  124. //////////////////////////////////////////////////////////////////////
  125. }