def show_task_lists(r: Redis) -> None: f = open(f'proactive_stats/stats.txt', "w+") todo = [] todo = r.lrange(name=TODO_TASK_LIST, start=0, end=-1) done = [] done = r.lrange(name=DONE_TASK_LIST, start=0, end=-1) string = f'To-do list:\n' for i in todo: string += f'{i}\n' string += f'\nDone list:\n' for i in done: string += f'{i}\n' f.write(string) f.close()
def lrange(self, name, start, end): """ Return a slice of the list ``name`` between position ``start`` and ``end`` ``start`` and ``end`` can be negative numbers just like Python slicing notation decode the results """ results = Redis.lrange(self, name, start, end) return [result.decode(self.encoding) for result in results]
class Jedis(): def __init__(self): pool = ConnectionPool(host=cfg.REDIS_HOST, port=cfg.REDIS_PORT) self.client = Redis(connection_pool=pool) def page_query(self, redis_key, start, end): ''' 分页查询 :param redis_key: redis中的key名称 :param start:起始页 :param end:结束页 :return: ''' return self.client.lrange(name=redis_key, start=start, end=end) def len(self, redis_key): return self.client.llen(name=redis_key) def lpush(self, key, val): self.client.lpush(key, val)
exit(0) # import os # print(os.listdir('../data-temp/')) # exit(90) # for key in red.keys('program-temp-*'): # red.delete(key) # exit(0) id = 25241424977985538 count = 0 for key in red.keys(): value = None if red.type(key) == b'set': value = red.smembers(key) if red.type(key) == b'list': value = red.lrange(key, 0, -1) count += len(value) # for item in value: # item = int(item) # if item == id: # print(key) print(count) exit(0) l1 = [1, 2, 3, 4, 9, 10, 11, 12] l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] for item1 in l1: for item2 in l2: j = judge_req_times() is_req_danmaku(item2, item1, j) # c = consumer() # produce(c, item2, item1)
class TSStore(object): def __init__(self, config): self._redis = Redis(host=config.get('redis','host'), port=int(config.get('redis','port')), db=int(config.get('redis','db'))) self._delta_secs = int(eval(config.get('timeseries', 'delta_secs'))) self._expiration_delay_secs = int(eval(config.get('timeseries', 'expiration_delay_secs'))) def queries_key(self): return 'queries' @property def queries(self): return self._redis.smembers(self.queries_key()) @queries.setter def queries(self, values): pipe = self._redis.pipeline() pipe.delete(self.queries_key()) for v in values: pipe.sadd(self.queries_key(), v) return pipe.execute() def _interval_key(self, timestamp): return int(timestamp) - int(timestamp) % self._delta_secs def _ts_key(self, timestamp, query): return 'ts:%(query)s:%(timestamp_key)s'%{'query':query, 'timestamp_key':self._interval_key(timestamp)} def _tweet_key(self, t): if type(t) == Tweet: return 'tweet:%s'%t.id return 'tweet:%s'%t def _query_key(self, query): return 'query:%s:last_tweet_id'%query def _store_tweet(self, pipe, tweet): tweet_key = self._tweet_key(tweet) pipe.set(tweet_key, tweet.serialize()) pipe.expire(tweet_key, self._expiration_delay_secs) def _reference_tweet(self, pipe, timestamp, query, tweet): ts_key = self._ts_key(timestamp, query) pipe.lpush(ts_key,tweet.id) pipe.expire(ts_key,self._expiration_delay_secs) def _update_last_query_tweet(self, pipe, query, tweet): query_key = self._query_key(query) pipe.set(query_key,tweet.id) def append(self, query, tweet): pipe = self._redis.pipeline() timestamp = time.time() self._store_tweet(pipe, tweet) self._reference_tweet(pipe, timestamp, query, tweet) self._update_last_query_tweet(pipe, query, tweet) return pipe.execute() def retrieve_ts(self, query, timestamp, n_elements=-1): ts_key = self._ts_key(timestamp, query) return self._redis.lrange(ts_key, 0, n_elements) def retrieve_last_tweet_id(self, query): query_key = self._query_key(query) return self._redis.get(query_key) def retrieve_tweet(self, tweet_id): tweet_key = self._tweet_key(tweet_id) data = self._redis.get(tweet_key) return Tweet.deserialize(data).todict() def retrieve(self, query, n_periods=30): current_timestamp = now = int(time.time()) start_timestamp = now - self._delta_secs * n_periods tweets = [] while current_timestamp > start_timestamp: current_tweet_ids = self.retrieve_ts(query, current_timestamp) tweets.append({'timestamp': current_timestamp, 'tweets' : [ self.retrieve_tweet(tid) for tid in current_tweet_ids ] }) current_timestamp -= self._delta_secs return { 'now' : now, 'ts' : tweets }