welcomecenterbot/bot/storage/__init__.py
2023-09-11 21:40:24 +03:00

35 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from redis import Redis
from bot.storage.profile import Profile as ProfileObj
from bot.config import REDIS_URL
import json
# сохраняет сессии, айди кнопок в чатах для удаления и пересылаемые сообщения между перезагрузками
storage = Redis.from_url(REDIS_URL)
# хранение необходимой информации о пользователях
Profile = ProfileObj(storage)
# достаёт из хранилища jsonы по маске и количеству
def scan(match='usr-*', count=100):
cursor = 0
keys = []
r = storage
while True:
# Scan for keys starting with <match> in batches of <count>
cursor, batch_keys = r.scan(cursor=cursor, match=match, count=count)
keys += batch_keys
# If the cursor is 0, then we've reached the end of the keys
if cursor == 0:
break
# Get the values of all the keys
values = r.mget(keys)
# Parse the JSON data from each value
items = []
for value in values:
value_str = value.decode('utf-8')
i = json.loads(value_str)
items.append(i)
print(f'scan found {len(items)} items')
return keys, items