基于Boost.MultiIndex实现的Session管理器

#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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值