基于Boost.MultiIndex实现的Session管理器
2014-08-03 17:53
405 查看
#ifndef SESSION_MANAGER_ #define SESSION_MANAGER_ #include <boost/multi_index_container.hpp> #include <boost/multi_index/hashed_index.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/member.hpp> #include <stdint.h> template <typename T> class SessionManager { public: SessionManager(size_t max_size = 10000, uint64_t timeout = 3) : max_size_(max_size), timeout_(timeout) {} private: SessionManager(const SessionManager &); SessionManager & operator = (const SessionManager &); public: int Init(size_t max_size, uint64_t timeout) { storage_.clear(); max_size_ = max_size; timeout_ = timeout; return 0; } int Insert(uint64_t id, uint64_t time, const T &entry, bool force = false) { IdIndex &index = storage_.template get<IdTag>(); typename IdIndex::iterator iter = index.find(id); if (iter != index.end()) return -1; if (index.size() >= max_size_) { if (!force) return -2; TimeIndex &time_index = storage_.template get<TimeTag>(); time_index.erase(time_index.begin()); } index.insert(Entry(id, time, entry)); return 0; } int Fetch(uint64_t id, uint64_t time, T &entry) { IdIndex &index = storage_.template get<IdTag>(); typename IdIndex::iterator iter = index.find(id); if (iter == index.end()) return -1; if (time != 0) index.modify(iter, TimeModifier(time)); entry = iter->entry; return 0; } void Delete(uint64_t id) { IdIndex &index = storage_.template get<IdTag>(); index.erase(id); } void Scan(uint64_t time) { TimeIndex &index = storage_.template get<TimeTag>(); for (typename TimeIndex::iterator iter = index.begin(); iter != index.end(); ) { if (iter->time + timeout_ > time) return; iter = index.erase(iter); } } size_t Size() const { return storage_.size(); } private: struct Entry { uint64_t id; uint64_t time; T entry; Entry(uint64_t id, uint64_t time, const T &entry) : id(id), time(time), entry(entry) {} }; struct TimeModifier { int new_time; TimeModifier(int new_time) : new_time(new_time) {} void operator () (Entry &entry) { entry.time = new_time; } }; struct IdTag {}; struct TimeTag {}; typedef boost::multi_index_container< Entry, boost::multi_index::indexed_by< boost::multi_index::hashed_unique<boost::multi_index::tag<IdTag>, boost::multi_index::member<Entry, uint64_t, &Entry::id> >, boost::multi_index::ordered_non_unique<boost::multi_index::tag<TimeTag>, boost::multi_index::member<Entry, uint64_t, &Entry::time> > > > Storage; typedef typename Storage::template index<IdTag>::type IdIndex; typedef typename Storage::template index<TimeTag>::type TimeIndex; private: Storage storage_; size_t max_size_; uint64_t timeout_; }; #endif
相关文章推荐
- UindexFTP基于ICS实现的FTP操作类[转]
- 基于Boost::asio库的网络编程实现基础
- 机器学习技法实现(一):AdaBoost- Decision Stump (AdaBoost - 决策树的基于Matlab的实现)
- 基于Hibernate实现多租户(Multi-Tendency)功能
- Objective-C实现一个基于数组下标[index]和字典[@"key"]的访问模式
- 基于ms index server的全文索引实现。
- 基于BootStrap multiselect.js实现的下拉框联动效果
- 基于boost 的苹果apns消息推送实现(1)
- 基于Boost无锁队列实现的内存池
- Gradient Tree Boosting (GBM, GBRT, GBDT, MART)算法解析和基于XGBoost/Scikit-learn的实现
- 多索引容器boost::multi_index_container储存共享智能指针boost::shared_ptr
- 用 boost::multi_index 管理玩家
- 用 boost::multi_index 管理玩家
- 一个C++基于boost简单实现的线程池
- 基于Boost::asio库的网络编程实现基础
- 基于Boost::asio库的网络编程实现基础
- 基于Boost::asio库的网络编程实现基础
- 基于CSS3的CSS 多栏(Multi-column)实现瀑布流源码分享
- Boost练习程序(multi_index_container)
- 使用boost::multi_index高速构建排行榜