1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
import asyncio
import logging
import random
from aiocache import Cache
from aiocache.lock import OptimisticLock, OptimisticLockError
logger = logging.getLogger(__name__)
cache = Cache(Cache.REDIS, endpoint='127.0.0.1', port=6379, namespace='main')
async def expensive_function():
logger.warning('Expensive is being executed...')
await asyncio.sleep(random.uniform(0, 2))
return 'result'
async def my_view():
async with OptimisticLock(cache, 'key') as lock:
result = await expensive_function()
try:
await lock.cas(result)
except OptimisticLockError:
logger.warning(
'I failed setting the value because it is different since the lock started!')
return result
async def concurrent():
await cache.set('key', 'initial_value')
# All three calls will read 'initial_value' as the value to check and only
# the first one finishing will succeed because the others, when trying to set
# the value, will see that the value is not the same as when the lock started
await asyncio.gather(my_view(), my_view(), my_view())
async def test_redis():
await concurrent()
await cache.delete("key")
await cache.close()
if __name__ == '__main__':
asyncio.run(test_redis())
|