cache: MapCache: Introduce MapCache, thread-safe cache (key/value store)
这个提交包含在:
		
							
								
								
									
										1
									
								
								cache/mapcache.cpp
									
									
									
									
										vendored
									
									
										普通文件
									
								
							
							
						
						
									
										1
									
								
								cache/mapcache.cpp
									
									
									
									
										vendored
									
									
										普通文件
									
								
							| @@ -0,0 +1 @@ | |||||||
|  | #include "mapcache.h" | ||||||
							
								
								
									
										37
									
								
								cache/mapcache.h
									
									
									
									
										vendored
									
									
										普通文件
									
								
							
							
						
						
									
										37
									
								
								cache/mapcache.h
									
									
									
									
										vendored
									
									
										普通文件
									
								
							| @@ -0,0 +1,37 @@ | |||||||
|  | #ifndef MAPCACHE_H | ||||||
|  | #define MAPCACHE_H | ||||||
|  | #include <map> | ||||||
|  | #include <set> | ||||||
|  | #include <shared_mutex> | ||||||
|  |  | ||||||
|  | /* Thread-Safe Key-Value store */ | ||||||
|  | template <class T> class MapCache | ||||||
|  | { | ||||||
|  |   private: | ||||||
|  | 	std::map<std::string, T> cache; | ||||||
|  | 	mutable std::shared_mutex sharedMutex; | ||||||
|  |  | ||||||
|  |   public: | ||||||
|  | 	std::optional<T> find(const std::string &key) const | ||||||
|  | 	{ | ||||||
|  | 		std::shared_lock<std::shared_mutex> lock(this->sharedMutex); | ||||||
|  | 		auto it = this->cache.find(key); | ||||||
|  | 		if(it != this->cache.end()) | ||||||
|  | 		{ | ||||||
|  | 			return it->second; | ||||||
|  | 		} | ||||||
|  | 		return {}; | ||||||
|  | 	} | ||||||
|  | 	void set(const std::string &key, const T &val) | ||||||
|  | 	{ | ||||||
|  | 		std::lock_guard<std::shared_mutex> lock{sharedMutex}; | ||||||
|  | 		this->cache[key] = val; | ||||||
|  | 	} | ||||||
|  | 	void clear() | ||||||
|  | 	{ | ||||||
|  | 		std::lock_guard<std::shared_mutex> lock{sharedMutex}; | ||||||
|  | 		this->cache.clear(); | ||||||
|  | 	} | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | #endif // MAPCACHE_H | ||||||
		在新工单中引用
	
	屏蔽一个用户