123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #include "servant/AppCache.h"
- #include "servant/Communicator.h"
- namespace tars
- {
- //////////////////////////////////////////////////////////////////////
- // 缓存
- void AppCache::setCacheInfo(const string &sFile,int32_t iSynInterval)
- {
- try
- {
- TC_LockT<TC_ThreadMutex> lock(*this);
- string sPath = TC_File::extractFilePath(sFile);
- TC_File::makeDirRecursive(sPath);
- _file = sFile;
- _synInterval = iSynInterval;
- if (TC_File::isFileExistEx(_file))
- {
- _fileCache.parseFile(_file);
- }
- //如果是旧版本数据(无版本号)直接清理
- if (_fileCache.get(string(APPCACHE_ROOT_PATH)+"<tarsversion>","") == "")
- {
- TC_Config tFileCache;
- _fileCache = tFileCache;
- }
- }
- catch(exception &e)
- {
- TLOGERROR("[TARS][AppCache setCacheInfo ex:" << e.what() << "]" << endl);
- }
- }
- string AppCache::get(const string & sName, const string sDomain)
- {
- if(_file.empty())
- {
- return "";
- }
- try
- {
- TC_LockT<TC_ThreadMutex> lock(*this);
- string sValue = _fileCache.get(string(APPCACHE_ROOT_PATH) + "/" + sDomain + "<" + sName + ">");
- return sValue;
- }
- catch(exception &e)
- {
- TLOGERROR("[TARS][AppCache get sName:" << sName << ",ex:" << e.what() << "]" << endl);
- }
- return "";
- }
- map<string, string> AppCache::getDomainMap(const string &path)
- {
- map<string, string> m;
- if(_file.empty())
- {
- return m;
- }
- try
- {
- TC_LockT<TC_ThreadMutex> lock(*this);
- m = _fileCache.getDomainMap(string(APPCACHE_ROOT_PATH) + "/" + path);
- }
- catch(exception &e)
- {
- TLOGERROR("[TARS][AppCache getDomainMap path:" << path << ",ex:" << e.what() << "]" << endl);
- }
- return m;
- }
- int AppCache::set(const string &sName,const string &sValue,const string sDomain)
- {
- if(_file.empty())
- {
- return -1;
- }
- try
- {
- TC_LockT<TC_ThreadMutex> lock(*this);
- map<string, string> m;
- m[sName] = sValue;
- TC_Config tConf;
- tConf.insertDomainParam(string(APPCACHE_ROOT_PATH)+"/"+sDomain,m,true);
- if(_lastSynTime == 0) //第一次写数据 打印tarsversion
- {
- m.clear();
- m["tarsversion"] = ClientConfig::TarsVersion;
- tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
- }
- {
- m.clear();
- m["modify"] = TC_Common::now2str("%Y-%m-%d %H:%M:%S");
- tConf.insertDomainParam(string(APPCACHE_ROOT_PATH),m,true);
- }
- _fileCache.joinConfig(tConf,true);
- time_t now = TNOW;
- if(_lastSynTime + _synInterval/1000 > now)
- {
- return 0;
- }
- _lastSynTime = now;
- TC_File::save2file(_file,_fileCache.tostr());
- return 0;
- }
- catch(exception &e)
- {
- TLOGERROR("[TARS][AppCache set name:" << sName << ",value:" << sValue << ",ex:" << e.what() << "]" << endl);
- }
- return -1;
- }
- //////////////////////////////////////////////////////////////////////
- }
|