示例#1
0
void process_cache::store(string const &key,set<string> const &triggers_in,time_t timeout_in,archive const &a)
{
	rwlock_wrlock lock(access_lock);
	pointer main;
	main=primary.find(key.c_str());

	if(main!=primary.end())
		delete_node(main);

	if(a.get().size()>memsize/20) {
		return;
	}

	time_t now;
	time(&now);
	// Make sure there is at least 10% avalible
	// And there is a block that is big enough to allocate 5% of memory
	for(;;) {
		if(process_cache_factory::mem->available() > memsize / 10) {
			void *p=process_cache_factory::mem->malloc(memsize/20);
			if(p) {
				process_cache_factory::mem->free(p);
				break;
			}
		}
		if(timeout.begin()->first<now) {
			main=timeout.begin()->second;
		}
		else {
			main=lru.back();
		}
		delete_node(main);
	}

	try {
		pair<pointer,bool> res=primary.insert(pair<shr_string,container>(key.c_str(),container()));

		main=res.first;
		container &cont=main->second;
		cont.data.assign(a.get().c_str(),a.get().size());

		lru.push_front(main);
		cont.lru=lru.begin();
		cont.timeout=timeout.insert(pair<time_t,pointer>(timeout_in,main));
		if(triggers_in.find(key)==triggers_in.end()){
			cont.triggers.push_back(triggers.insert(
					pair<shr_string,pointer>(key.c_str(),main)));
		}
		set<string>::const_iterator si;
		for(si=triggers_in.begin();si!=triggers_in.end();si++) {
			cont.triggers.push_back(triggers.insert(
				pair<shr_string,pointer>(si->c_str(),main)));
		}
	}
	catch(std::bad_alloc const &e) {
		clear();
	}
}
示例#2
0
void thread_cache::store(string const &key,set<string> const &triggers_in,time_t timeout_in,archive const &a)
{
	rwlock_wrlock lock(access_lock);
	if(debug_mode)	print_all();
	pointer main;
	if(debug_mode) {
		string res;
		res=str(boost::format("Storing key [%1%], triggers:") % key);
		for(set<string>::iterator ps=triggers_in.begin(),pe=triggers_in.end();ps!=pe;ps++) {
			res+=*ps;
			res+=" ";
		}
		res+="\n";
		write(fd,res.c_str(),res.size());
	}
	main=primary.find(key);
	if(main==primary.end() && primary.size()>=limit && limit>0) {
		if(debug_mode) {
			char const *msg="Not found, size limit\n";
			write(fd,msg,strlen(msg));
		}
		time_t now;
		time(&now);
		if(timeout.begin()->first<now) {
			main=timeout.begin()->second;
			if(debug_mode) {
				string res;
				res=str(boost::format("Deleting timeout node [%1%] with "
							"delta  of %2% seconds\n") % main->first
							% (now - main->second.timeout->first));
				write(fd,res.c_str(),res.size());
			}
		}
		else {
			main=lru.back();
			if(debug_mode) {
				string res;
				res=str(boost::format("Deleting LRU [%1%]\n") % main->first);
				write(fd,res.c_str(),res.size());
			}
		}
	}
	if(main!=primary.end())
		delete_node(main);
	pair<pointer,bool> res=primary.insert(pair<string,container>(key,container()));
	main=res.first;
	container &cont=main->second;
	cont.data=a.get();
	lru.push_front(main);
	cont.lru=lru.begin();
	cont.timeout=timeout.insert(pair<time_t,pointer>(timeout_in,main));
	if(triggers_in.find(key)==triggers_in.end()){
		cont.triggers.push_back(triggers.insert(pair<string,pointer>(key,main)));
	}
	set<string>::const_iterator si;
	for(si=triggers_in.begin();si!=triggers_in.end();si++) {
		cont.triggers.push_back(triggers.insert(pair<string,pointer>(*si,main)));
	}
}