Study/django

django-redis에서 scan_iter를 count 옵션과 함께 사용하는 방법

bluebamus 2023. 3. 21.

1. 1000개 단위로 모든 키 가져오기:

from django.core.cache import cache

keys = []
for key in cache.scan_iter("*", count=1000):
    keys.append(key)
if keys:
    # Do something with the keys

이 코드는 scan_iter()를 사용하여 1000개의 배치로 모든 키를 검색하고 목록에 추가합니다. 모든 키가 검색되면 원하는 대로 처리할 수 있습니다.


2. 패턴과 일치하는 모든 키를 500개 배치로 가져옵니다.

from django.core.cache import cache

keys = []
for key in cache.scan_iter("my_prefix:*", count=500):
    keys.append(key)
if keys:
    # Do something with the keys

이 코드는 scan_iter()를 사용하여 500개의 배치에서 "my_prefix:*" 패턴과 일치하는 모든 키를 검색하고 목록에 추가합니다.

 

3. 250개의 배치에서 모든 키를 삭제합니다.

from django.core.cache import cache

keys = []
for key in cache.scan_iter("*", count=250):
    keys.append(key)
    if len(keys) == 250:
        cache.delete_many(keys)
        keys = []
if keys:
    cache.delete_many(keys)

이 코드는 scan_iter() 및 delete_many()를 사용하여 250개의 배치에서 모든 키를 삭제합니다. 각 키를 목록에 추가하고 목록이 일정 크기(이 경우 250개 키)에 도달하면 delete_many()를 사용하여 한 번에 모두 삭제합니다.

 

4. TTL이 60초 미만인 모든 키를 100개 일괄 처리로 가져옵니다.

from django.core.cache import cache

keys = []
for key in cache.scan_iter("*", count=100):
    ttl = cache.ttl(key)
    if ttl is not None and ttl < 60:
        keys.append(key)
if keys:
    # Do something with the keys

이 코드는 scan_iter()를 사용하여 100개의 배치로 모든 키를 검색하고 ttl()을 사용하여 각 키의 TTL(time-to-live)을 확인합니다. TTL이 60초 미만이면 목록에 키를 추가합니다.

 

5. 지난 1시간 동안 수정된 모든 키를 500개 일괄 처리로 가져옵니다.

from django.core.cache import cache
from datetime import datetime, timedelta

keys = []
for key in cache.scan_iter("*", count=500):
    timestamp = cache.get(key + "_timestamp")
    if timestamp is not None and datetime.now() - datetime.fromtimestamp(timestamp) < timedelta(hours=1):
        keys.append(key)
if keys:
    # Do something with the keys

이 코드는 scan_iter()를 사용하여 500개의 배치에서 모든 키를 검색하고 get()을 사용하여 각 키의 타임스탬프를 확인합니다. 키가 지난 1시간 이내에 수정된 경우 목록에 추가합니다. 이 코드는 각 값의 타임스탬프를 저장하기 위해 별도의 키를 사용한다고 가정합니다. 이는 캐시 만료를 구현하기 위한 일반적인 패턴입니다.


6. 다음은 Redis 트랜잭션을 사용하고 해당 값을 설정하기 전에 키가 이미 존재하지 않는지 확인하는 예제 코드입니다.

import redis

# create Redis client and initialize pipeline
r = redis.Redis(host='localhost', port=6379, db=0)
pipeline = r.pipeline()

# define key and value to be set
key = 'my_key'
value = 'my_value'

# start transaction
while True:
    try:
        pipeline.watch(key)
        if not r.exists(key):
            pipeline.multi()
            pipeline.set(key, value)
            pipeline.execute()
            break
        else:
            pipeline.reset()
            break
    except redis.exceptions.WatchError:
        continue

이 코드에서는 먼저 Redis 클라이언트를 생성하고 파이프라인을 초기화합니다. 그런 다음 설정하려는 키와 값을 정의합니다. Redis 트랜잭션을 시작하고 키-값 쌍 설정을 시도합니다.

먼저 watch 명령을 사용하여 키를 모니터링합니다. 키가 아직 존재하지 않으면 multi로 트랜잭션을 시작하고 set으로 키-값 쌍을 설정하고 execute로 트랜잭션을 실행합니다.

키가 이미 존재하는 경우 reset으로 파이프라인을 재설정하고 루프를 중단합니다. 트랜잭션 중에 WatchError가 발생하면 이를 포착하고 루프를 다시 시도합니다.

이 코드는 키가 이미 존재하지 않는 경우에만 키가 설정되도록 하고 Redis 트랜잭션을 사용하여 안전한 실행을 보장합니다.

 

7. 트랜잭션을 사용하여 Redis 데이터베이스에서 제한된 수의 키를 삭제합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=count)
    for key in keys:
        pipe.delete(key)
    result = pipe.execute()
print(result)

8. 트랜잭션을 사용하여 Redis 세트에서 제한된 수의 항목 가져오기:

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
set_name = 'my_set'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    items = redis_conn.zrange(set_name, 0, count - 1)
    result = pipe.execute()
print(result[0])

9. 트랜잭션을 사용하여 Redis 세트에 제한된 수의 항목을 추가합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
set_name = 'my_set'
items = ['item1', 'item2', 'item3']
count = len(items)
with redis_conn.pipeline(transaction=True) as pipe:
    for i in range(count):
        pipe.zadd(set_name, 1.0, items[i])
    result = pipe.execute()
print(result)

10. 트랜잭션을 사용하여 Redis 목록에서 제한된 수의 항목을 삭제합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
list_name = 'my_list'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    items = redis_conn.lrange(list_name, 0, count - 1)
    for item in items:
        pipe.lrem(list_name, 0, item)
    result = pipe.execute()
print(result)

11. 트랜잭션을 사용하여 한 Redis 목록에서 다른 목록으로 제한된 수의 항목을 이동합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
source_list = 'my_list1'
destination_list = 'my_list2'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    items = redis_conn.lrange(source_list, 0, count - 1)
    for item in items:
        pipe.lrem(source_list, 0, item)
        pipe.lpush(destination_list, item)
    result = pipe.execute()
print(result)

12. 트랜잭션을 사용하여 Redis 해시에서 제한된 수의 항목 가져오기:

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
hash_name = 'my_hash'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.hkeys(hash_name)
    for key in keys[:count]:
        pipe.hget(hash_name, key)
    result = pipe.execute()
print(result)

13.  트랜잭션을 사용하여 제한된 수의 항목을 Redis 해시에 추가합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
hash_name = 'my_hash'
items = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
count = len(items)
with redis_conn.pipeline(transaction=True) as pipe:
    for key, value in items.items():
        pipe.hset(hash_name, key, value)
    result = pipe.execute()
print(result)

14. 트랜잭션을 사용하여 여러 Redis 데이터베이스에서 제한된 수의 키를 삭제합니다.

from django_redis import get_redis_connection

redis_conn1 = get_redis_connection('default')
redis_conn2 = get_redis_connection('secondary')
key_pattern = 'my_key_*'
count = 100
with redis_conn1.pipeline(transaction=True) as pipe1, redis_conn2.pipeline(transaction=True) as pipe2:
    keys1 = redis_conn1.scan_iter(key_pattern, count=count)
    keys2 = redis_conn2.scan_iter(key_pattern, count=count)
    for key in keys1:
        pipe1.delete(key)
    for key in keys2:
        pipe2.delete(key)
    result1, result2 = pipe1.execute(), pipe2.execute()
print(result1, result2)

15. 트랜잭션을 사용하여 한 Redis 세트에서 다른 Redis 세트로 제한된 수의 항목을 이동합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
source_set = 'my_set1'
destination_set = 'my_set2'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    items = redis_conn.zrange(source_set, 0, count - 1)
    for item in items:
        pipe.zrem(source_set, item)
        pipe.zadd(destination_set, {item: 1.0})
    result = pipe.execute()
print(result)

이 예제는 여러 데이터베이스에서 Redis 데이터 구조의 항목 추가, 삭제 및 이동을 포함하여 다양한 시나리오에서 안전한 실행을 위해 개수 및 Redis 트랜잭션과 함께 'cache.scan_iter()'를 사용하는 방법을 보여줍니다. 트랜잭션을 사용하면 코드 실행 중에 오류가 발생하더라도 각 작업이 원자적이며 데이터베이스가 일관된 상태로 유지됩니다.

16. count와 함께 cache.scan_iter()를 사용하여 Redis의 모든 키를 반복하고 각 키에서 작업을 수행합니다

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=count)
    for key in keys:
        pipe.incr(key)
    result = pipe.execute()
print(result)

17. count와 함께 cache.scan_iter()를 사용하여 Redis에서 키 하위 집합을 반복하고 각 키에서 작업을 수행합니다

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
start = 0
end = 99
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=end-start+1, match=key_pattern)
    for key in keys:
        pipe.incr(key)
    result = pipe.execute()
print(result)

18. count와 함께 cache.scan_iter()를 사용하여 대규모 Redis 키 집합을 반복하고 각 키를 삭제합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=count)
    for key in keys:
        pipe.delete(key)
    result = pipe.execute()
print(result)

19. count와 함께 cache.scan_iter()를 사용하여 Redis 키의 하위 집합을 반복하고 각 키를 삭제합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
start = 0
end = 99
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=end-start+1, match=key_pattern)
    for key in keys:
        pipe.delete(key)
    result = pipe.execute()
print(result)

20. count와 함께 cache.scan_iter()를 사용하여 대규모 Redis 키 집합을 반복하고 Redis 집합에 추가합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
destination_set = 'my_set'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=count)
    for key in keys:
        pipe.sadd(destination_set, key)
    result = pipe.execute()
print(result)

21. count와 함께 cache.scan_iter()를 사용하여 Redis 키의 하위 집합을 반복하고 Redis 세트에 추가합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
destination_set = 'my_set'
start = 0
end = 99
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=end-start+1, match=key_pattern)
    for key in keys:
        pipe.sadd(destination_set, key)
    result = pipe.execute()
print(result)

22. count와 함께 cache.scan_iter()를 사용하여 대규모 Redis 키 집합을 반복하고 Redis 정렬 집합으로 이동합니다.

from django_redis import get_redis_connection

redis_conn = get_redis_connection()
key_pattern = 'my_key_*'
destination_sorted_set = 'my_sorted_set'
count = 100
with redis_conn.pipeline(transaction=True) as pipe:
    keys = redis_conn.scan_iter(key_pattern, count=count)

댓글