template<typename T>
class StupidCacheKV
{
private:
map<string, T> cache;
mutable sox::mutex StupidCacheMutex;
public:
void set(const string& key, const T & value)
{
sox::scope_lock lock(StupidCacheMutex);
cache[key] = value;
};
T get(const string& key)
{
sox::scope_lock lock(StupidCacheMutex);
return cache[key];
};
bool exist(const string& key) const
{
sox::scope_lock lock(StupidCacheMutex);
return (cache.find(key) != cache.end());
}
uint32_t size() const
{
sox::scope_lock lock(StupidCacheMutex);
return cache.size();
}
};
class StupidCacheKV
{
private:
map<string, T> cache;
mutable sox::mutex StupidCacheMutex;
public:
void set(const string& key, const T & value)
{
sox::scope_lock lock(StupidCacheMutex);
cache[key] = value;
};
T get(const string& key)
{
sox::scope_lock lock(StupidCacheMutex);
return cache[key];
};
bool exist(const string& key) const
{
sox::scope_lock lock(StupidCacheMutex);
return (cache.find(key) != cache.end());
}
uint32_t size() const
{
sox::scope_lock lock(StupidCacheMutex);
return cache.size();
}
};
本文介绍了一个通用模板缓存类StupidCacheKV的设计与实现。该类使用了互斥锁来保证多线程环境下数据的一致性和安全性,并提供了设置、获取缓存项及检查缓存项是否存在等基本操作。
156

被折叠的 条评论
为什么被折叠?



