コード例 #1
0
ファイル: ttl.cpp プロジェクト: sunxiaojun2014/qssdb
void* ExpirationHandler::thread_func(void *arg){
	ExpirationHandler *handler = (ExpirationHandler *)arg;
	
	int64_t last_load_empty_times = 0;
	while(!handler->thread_quit){
		SSDB *ssdb = handler->ssdb;
		if(!ssdb){
			break;
		}
		if(!handler->enable){
			usleep(10 * 1000);
			continue;
		}
		int64_t current = time_ms();
		if(handler->fast_keys.empty() && (current - last_load_empty_times) < ADVANCE_READ_TTL * 1000){
			usleep(10 * 1000);
			continue;
		}
		if(!handler->fast_keys.empty() && handler->first_timeout > current){
			usleep(10 * 1000);
			continue;
		}
		{
			Locking l(&handler->mutex);
			if(!handler->enable){
			    continue;
			}
            // TOODO 增加最大值的限制
			if(handler->fast_keys.empty()){
				int64_t expired = current + ADVANCE_READ_TTL * 1000;
				handler->load_expiration_keys_from_db(expired, BATCH_SIZE);
				handler->first_timeout = INT64_MAX;
				if(handler->fast_keys.empty()) {
				    last_load_empty_times = current;
				}
			}
		
			int64_t score;
			std::string key;
			if(handler->fast_keys.front(&key, &score)){
				handler->first_timeout = score;
				
				if(score <= current){
					log_debug("expired %s", key.c_str());
					ssdb->del(key);
					ssdb->zdel(handler->list_name, key);
					handler->fast_keys.pop_front();
				}
			}
		}
	}
	
	log_debug("ExpirationHandler thread quit");
	handler->thread_quit = false;
	return (void *)NULL;
}
コード例 #2
0
ファイル: ttl.cpp プロジェクト: beykery/ssdb
void* ExpirationHandler::thread_func(void *arg){
	ExpirationHandler *handler = (ExpirationHandler *)arg;
	
	while(!handler->thread_quit){
		SSDB *ssdb = handler->ssdb;
		if(!ssdb){
			break;
		}
		if(handler->first_timeout > time_ms()){
			usleep(10 * 1000);
			continue;
		}
		
		{
			Locking l(&handler->mutex);
			if(handler->fast_keys.empty()){
				handler->load_expiration_keys_from_db(BATCH_SIZE);
				handler->first_timeout = INT64_MAX;
			}
		
			int64_t score;
			std::string key;
			if(handler->fast_keys.front(&key, &score)){
				handler->first_timeout = score;
				
				if(score <= time_ms()){
					log_debug("expired %s", key.c_str());
					ssdb->del(key);
					ssdb->zdel(handler->list_name, key);
					handler->fast_keys.pop_front();
				}
			}
		}
	}
	
	log_debug("ExpirationHandler thread quit");
	handler->thread_quit = false;
	return (void *)NULL;
}