summaryrefslogtreecommitdiff
path: root/extdata.c
diff options
context:
space:
mode:
Diffstat (limited to 'extdata.c')
-rw-r--r--extdata.c54
1 files changed, 31 insertions, 23 deletions
diff --git a/extdata.c b/extdata.c
index 01107c1..f71f410 100644
--- a/extdata.c
+++ b/extdata.c
@@ -36,53 +36,61 @@ cExtData::~cExtData(void) {
}
-bool cExtData::Set(std::string key, std::string value, uint32_t expire) {
+bool cExtData::Set(const std::string & key, const std::string & value, const std::string & scope, uint32_t expire ) {
if (mExtDataInstance == NULL) return false; // paranoia check
- data[key] = value;
+ std::string compkey = scope; compkey += ":"; compkey += key; // <scope>:<key>, <scope> can be "" (== global)
+ data[compkey] = value;
if (expire > 0) {
- expData[key] = cTimeMs::Now() + (expire * 1000);
+ expData[compkey] = cTimeMs::Now() + (expire * 1000);
} else {
- expData.erase(key); // just in case of an old expiration entry for key
+ expData.erase(compkey); // just in case of an old expiration entry for compkey
}
return true;
}
-bool cExtData::Unset(std::string key) {
+bool cExtData::Unset(const std::string & key, const std::string & scope ) {
if (mExtDataInstance == NULL) return false; // paranoia check
- expData.erase(key); // ignore result;
- return ( (data.erase(key) > 0) ? true : false );
+ std::string compkey = scope; compkey += ":"; compkey += key; // <scope>:<key>, <scope> can be "" (== global)
+ expData.erase(compkey); // ignore result;
+ return ( (data.erase(compkey) > 0) ? true : false );
}
-bool cExtData::IsSet(std::string key) {
- std::string ret = Get(key);
+bool cExtData::IsSet(const std::string & key, const std::string & scope ) {
+ std::string ret = Get(key, scope);
return ( (ret != "") ? true : false );
}
-std::string cExtData::Get(std::string key) {
+std::string cExtData::Get(const std::string & key, const std::string & scope ) {
if (mExtDataInstance == NULL) return ""; // paranoia check
- it = data.find(key);
- if ( it != data.end() ) {
- expDataIt = expData.find(key);
- if ( expDataIt != expData.end() ) {
- uint64_t expts = (*expDataIt).second;
- if ( cTimeMs::Now() > expts ) {
- expData.erase(key);
- data.erase(key);
- return "";
+ std::string compkey;
+ int l = 0;
+ int loops = (scope == "") ? 1 : 2;
+ while ( l < loops) {
+ compkey = (l == 0) ? scope : ""; compkey += ":"; compkey += key; // 1st loop: display-scope, 2nd loop: global
+ it = data.find(compkey);
+ if ( it != data.end() ) {
+ expDataIt = expData.find(compkey);
+ if ( expDataIt != expData.end() ) {
+ uint64_t expts = (*expDataIt).second;
+ if ( cTimeMs::Now() > expts ) {
+ expData.erase(compkey);
+ data.erase(compkey);
+ // expired, thus no result here, but maybe there'll be a hit in global scope, so don't return here
+ } else {
+ return (*it).second;
+ }
} else {
return (*it).second;
}
- } else {
- return (*it).second;
}
- } else {
- return "";
+ l++;
}
+ return "";
}