示例#1
0
void session::deserialize(string& buf, std::map<string, VBUF*>& attrs)
{
	attrs_clear(attrs);  // 先重置 session 前一次查询状态

	ACL_ARGV* tokens = acl_argv_split(buf.c_str(), "\t");
	ACL_ITER  iter;
	acl_foreach(iter, tokens)
	{
		char* ptr = (char*) iter.data;

		// 重复使用原来的内存区,因为 tokens 中已经存储了中间结果数据
		buf.clear();
		if (unescape(ptr, strlen(ptr), buf) == false)
		{
			logger_error("unescape error");
			continue;
		}
		ptr = buf.c_str();
		// 因为 acl::string 肯定能保证缓冲区数据的尾部有 \0,所以在用
		// strchr 时不必须担心越界问题,但 std::string 并不保证这样
		char* p1 = strchr(ptr, 1);
		if (p1 == NULL || *(p1 + 1) == 0)
			continue;
		*p1++ = 0;
		std::map<string, VBUF*>::iterator it = attrs.find(ptr);

		// xxx: 以防有重复的属性
		if (it != attrs.end())
			vbuf_free(it->second);
		// 将从后端取得数据属性都设为 TODO_SET
		attrs[ptr] = vbuf_new(p1, buf.length() - (p1 - buf.c_str()), TODO_SET);
	}
示例#2
0
void session::deserialize(string& buf, std::map<string, session_string>& attrs)
{
	attrs_clear(attrs);  // 先重置 session 前一次查询状态

	ACL_ARGV* tokens = acl_argv_split(buf.c_str(), "\t");
	ACL_ITER  iter;
	acl_foreach(iter, tokens)
	{
		char* ptr = (char*) iter.data;

		// 重复使用原来的内存区,因为 tokens 中已经存储了中间结果数据
		buf.clear();
		if (unescape(ptr, strlen(ptr), buf) == false)
		{
			logger_error("unescape error");
			continue;
		}
		ptr = buf.c_str();
		// 因为 acl::string 肯定能保证缓冲区数据的尾部有 \0,所以在用
		// strchr 时不必须担心越界问题,但 std::string 并不保证这样
		char* p1 = strchr(ptr, 1);
		if (p1 == NULL || *(p1 + 1) == 0)
			continue;
		*p1++ = 0;
		//std::map<string, session_string>::iterator it = attrs.find(ptr);

		size_t len = buf.length() - (p1 - buf.c_str());
		session_string ss(len);
		ss.copy(p1, len);
		ss.todo_ = TODO_SET;
		attrs.insert(std::make_pair(string(ptr), ss));
	}
示例#3
0
bool session::set(const char* name, const void* value, size_t len,
	bool delay /* = false */)
{
	if (delay)
	{
		std::map<string, VBUF*>::iterator it = attrs_cache_.find(name);
		if (it == attrs_cache_.end())
			attrs_cache_[name] = vbuf_new(value, len, TODO_SET);
		else
			attrs_cache_[name] = vbuf_set(it->second, value, len, TODO_SET);
		dirty_ = true;
		return true;
	}

	// 直接操作后端 cache 服务器,设置(添加/修改) 属性字段

	string buf(256);

	// 调用纯虚接口,获得原来的 sid 数据
	if (get_data(sid_->buf, buf) == false)
	{
		// 如果没有则创建新的 sid 数据
		serialize(name, value, len, buf);
	}

	// 如果存在对应 sid 的数据,则将新数据添加在原来数据中
	else
	{
		if (!sid_saved_)
			sid_saved_ = true;

		// 反序列化
		deserialize(buf, attrs_);

		// 如果该属性已存在,则需要先释放原来的属性值后再添加新值

		std::map<string, VBUF*>::iterator it = attrs_.find(name);
		if (it == attrs_.end())
			attrs_[name] = vbuf_new(value, len, TODO_SET);
		else
			attrs_[name] = vbuf_set(it->second, value, len, TODO_SET);
		serialize(attrs_, buf);  // 序列化数据
		attrs_clear(attrs_);
	}

	// 调用纯虚接口,向 memcached 或类似缓存中添加数据
	if (set_data(sid_->buf, buf.c_str(), buf.length(), ttl_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_->buf);
		return false;
	}
	if (!sid_saved_)
		sid_saved_ = true;
	return true;
}
示例#4
0
bool session::del(const char* name, bool delay /* = false */)
{
	if (delay)
	{
		std::map<string, VBUF*>::iterator it = attrs_cache_.find(name);
		if (it == attrs_cache_.end())
			attrs_cache_[name] = vbuf_new("", 0, TODO_DEL);
		else
			it->second->todo = TODO_DEL;
		dirty_ = true;
		return true;
	}

	// 直接操作后端 cache 服务器,删除属性字段

	string buf(256);
	if (get_data(sid_->buf, buf) == false)
		return true;

	deserialize(buf, attrs_);
	std::map<string, VBUF*>::iterator it = attrs_.find(name);
	if (it == attrs_.end())
		return false;

	// 先删除并释放对应的对象
	vbuf_free(it->second);
	attrs_.erase(it);

	// 如果 sid 中已经没有了数据,则应该将 sid 对象从 memcached 中删除
	if (attrs_.empty())
	{
		// 调用虚函数,删除该 sid 对应的缓存内容
		if (del_data(sid_->buf) == false)
		{
			logger_error("del sid(%s) error", sid_->buf);
			return false;
		}
		return true;
	}

	// 向 memcached 中重新添加剩余的数据

	serialize(attrs_, buf);
	attrs_clear(attrs_);

	if (set_data(sid_->buf, buf.c_str(), buf.length(), ttl_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_->buf);
		return false;
	}
	return true;
}
示例#5
0
bool session::del(const char* name)
{
	// 直接操作后端 cache 服务器,删除属性字段

	if (get_attrs(attrs_) == false)
		return true;

	std::map<string, session_string>::iterator it = attrs_.find(name);
	if (it == attrs_.end())
		return false;

	// 先删除并释放对应的对象
	attrs_.erase(it);

	// 如果 sid 中已经没有了数据,则应该将 sid 对象从 memcached 中删除
	if (attrs_.empty())
	{
		// 调用虚函数,删除该 sid 对应的缓存内容
		if (remove() == false)
		{
			logger_error("del sid(%s) error", sid_.c_str());
			return false;
		}
		return true;
	}

	// 重新添加剩余的数据

	if (set_attrs(attrs_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_.c_str());
		attrs_clear(attrs_);  // 清除属性集合数据

		return false;
	}
	attrs_clear(attrs_);  // 清除属性集合数据

	return true;
}
示例#6
0
bool session::set(const char* name, const void* value, size_t len)
{
	// 直接操作后端 cache 服务器,设置(添加/修改) 属性字段

	// 调用纯虚接口,获得原来的 sid 数据
	if (get_attrs(attrs_) == false)
	{
		session_string ss(len);
		ss.copy(value, len);
		ss.todo_ = TODO_SET;
		attrs_cache_.insert(std::make_pair(string(name), ss));
	}
	// 如果存在对应 sid 的数据,则将新数据添加在原来数据中
	else
	{
		if (!sid_saved_)
			sid_saved_ = true;

		// 如果该属性已存在,则需要先释放原来的属性值后再添加新值
		session_string ss(len);
		ss.copy(value, len);
		ss.todo_ = TODO_SET;
		attrs_cache_.insert(std::make_pair(string(name), ss));
	}

	// 调用纯虚接口,向 memcached 或类似缓存中添加数据
	if (set_attrs(attrs_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_.c_str());
		attrs_clear(attrs_);  // 清除属性集合数据

		return false;
	}
	attrs_clear(attrs_);  // 清除属性集合数据

	if (!sid_saved_)
		sid_saved_ = true;
	return true;
}
示例#7
0
bool memcache_session::get_attrs(std::map<string, session_string>& attrs)
{
	// 清空原有数据
	attrs_clear(attrs);
	const char* sid = get_sid();
	if (sid == NULL || *sid == 0)
		return false;

	string buf;
	if (cache_->get(sid, buf) == false)
		return false;

	// 反序列化
	deserialize(buf, attrs);
	return true;
}
示例#8
0
bool session::flush()
{
	if (!dirty_)
		return true;
	dirty_ = false;

	// 调用纯虚接口,获得原来的 sid 数据
	if (get_attrs(attrs_) == true)
	{
		if (!sid_saved_)
			sid_saved_ = true;
	}

	std::map<string, session_string>::iterator it_cache =
		attrs_cache_.begin();
	for (; it_cache != attrs_cache_.end(); ++it_cache)
	{
		// 如果该属性已存在,则需要先释放原来的属性值后再添加新值

		std::map<string, session_string>::iterator it_attr =
			attrs_.find(it_cache->first);
		if (it_attr == attrs_.end())
		{
			if (it_cache->second.todo_ == TODO_SET)
				attrs_.insert(std::make_pair(it_cache->first,
					it_cache->second));
		}
		else if (it_cache->second.todo_ == TODO_SET)
		{
			// 设置新的数据
			attrs_.insert(std::make_pair(it_cache->first,
				it_cache->second));
		}
		else if (it_cache->second.todo_ == TODO_DEL)
		{
			attrs_.erase(it_attr);
		}
		else
		{
			logger_warn("unknown todo(%d)",
				(int) it_cache->second.todo_);
		}
	}

	// 清除缓存的数据:因为内部的数据已经被添加至 attrs_ 中,
	// 所以只需要将 attrs_cache_ 空间清除即可
	attrs_cache_.clear();

	// 调用纯虚接口,向 memcached 或类似缓存中添加数据
	if (set_attrs(attrs_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_.c_str());
		attrs_clear(attrs_);  // 清除属性集合数据

		return false;
	}

	attrs_clear(attrs_);  // 清除属性集合数据

	if (!sid_saved_)
		sid_saved_ = true;
	return true;
}
示例#9
0
void session::reset()
{
	attrs_clear(attrs_);
	attrs_clear(attrs_cache_);
}
示例#10
0
bool session::flush()
{
	if (!dirty_)
		return true;
	dirty_ = false;

	string buf(256);

	// 调用纯虚接口,获得原来的 sid 数据
	if (get_data(sid_->buf, buf) == true)
	{
		if (!sid_saved_)
			sid_saved_ = true;
		deserialize(buf, attrs_);  // 反序列化
	}

	std::map<string, VBUF*>::iterator it_cache = attrs_cache_.begin();
	for (; it_cache != attrs_cache_.end(); ++it_cache)
	{
		// 如果该属性已存在,则需要先释放原来的属性值后再添加新值

		std::map<string, VBUF*>::iterator it_attr =
			attrs_.find(it_cache->first);
		if (it_attr == attrs_.end())
		{
			if (it_cache->second->todo == TODO_SET)
				attrs_[it_cache->first] = it_cache->second;
			else
				vbuf_free(it_cache->second);
		}
		else if (it_cache->second->todo == TODO_SET)
		{
			// 清除旧的数据
			vbuf_free(it_attr->second);
			// 设置新的数据
			attrs_[it_cache->first] = it_cache->second;
		}
		else if (it_cache->second->todo == TODO_DEL)
		{
			vbuf_free(it_attr->second);
			attrs_.erase(it_attr);
			vbuf_free(it_cache->second);
		}
		else
		{
			logger_warn("unknown todo(%d)", (int) it_cache->second->todo);
			vbuf_free(it_cache->second);
		}
	}

	// 清除缓存的数据:因为内部的数据已经被添加至 attrs_ 中,
	// 所以只需要将 attrs_cache_ 空间清除即可
	attrs_cache_.clear();

	serialize(attrs_, buf);  // 序列化数据
	attrs_clear(attrs_);  // 清除属性集合数据

	// 调用纯虚接口,向 memcached 或类似缓存中添加数据
	if (set_data(sid_->buf, buf.c_str(), buf.length(), ttl_) == false)
	{
		logger_error("set cache error, sid(%s)", sid_->buf);
		return false;
	}

	if (!sid_saved_)
		sid_saved_ = true;
	return true;
}