AppCache.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. TLOGERROR("[TARS][AppCache setCacheInfo ex:" << e.what() << "]" << endl);
  45. }
  46. }
  47. string AppCache::get(const string & sName, const string sDomain)
  48. {
  49. if(_file.empty())
  50. {
  51. return "";
  52. }
  53. try
  54. {
  55. TC_LockT<TC_ThreadMutex> lock(*this);
  56. string sValue = _fileCache.get(string(APPCACHE_ROOT_PATH) + "/" + sDomain + "<" + sName + ">");
  57. return sValue;
  58. }
  59. catch(exception &e)
  60. {
  61. TLOGERROR("[TARS][AppCache get sName:" << sName << ",ex:" << e.what() << "]" << endl);
  62. }
  63. return "";
  64. }
  65. map<string, string> AppCache::getDomainMap(const string &path)
  66. {
  67. map<string, string> m;
  68. if(_file.empty())
  69. {
  70. return m;
  71. }
  72. try
  73. {
  74. TC_LockT<TC_ThreadMutex> lock(*this);
  75. m = _fileCache.getDomainMap(string(APPCACHE_ROOT_PATH) + "/" + path);
  76. }
  77. catch(exception &e)
  78. {
  79. TLOGERROR("[TARS][AppCache getDomainMap path:" << path << ",ex:" << e.what() << "]" << endl);
  80. }
  81. return m;
  82. }
  83. int AppCache::set(const string &sName,const string &sValue,const string sDomain)
  84. {
  85. if(_file.empty())
  86. {
  87. return -1;
  88. }
  89. try
  90. {
  91. TC_LockT<TC_ThreadMutex> lock(*this);
  92. map<string, string> m;
  93. m[sName] = sValue;
  94. TC_Config tConf;
  95. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH)+"/"+sDomain,m,true);
  96. if(_lastSynTime == 0) //第一次写数据 打印tarsversion
  97. {
  98. m.clear();
  99. m["tarsversion"] = ClientConfig::TarsVersion;
  100. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
  101. }
  102. {
  103. m.clear();
  104. m["modify"] = TC_Common::now2str("%Y-%m-%d %H:%M:%S");
  105. tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
  106. }
  107. _fileCache.joinConfig(tConf,true);
  108. time_t now = TNOW;
  109. if(_lastSynTime + _synInterval/1000 > now)
  110. {
  111. return 0;
  112. }
  113. _lastSynTime = now;
  114. TC_File::save2file(_file,_fileCache.tostr());
  115. return 0;
  116. }
  117. catch(exception &e)
  118. {
  119. TLOGERROR("[TARS][AppCache set name:" << sName << ",value:" << sValue << ",ex:" << e.what() << "]" << endl);
  120. }
  121. return -1;
  122. }
  123. //////////////////////////////////////////////////////////////////////
  124. }