This example uses the cache-aside pattern where the application is responsible for populating the cache with data ( the first time any selection is made, the underlying service will be invoked )
This is the most common way to use Redis as a cache. With this strategy, the application first looks into the cache to retrieve the data. If data is not found (cache miss), the application then retrieves the data from the operational data store directly. Data is loaded to the cache only when necessary (hence: lazy-loading). Read-heavy applications can greatly benefit from implementing a cache-aside approach.
line 158 (uncomment the code here when you are ready to enable caching the individual ascii-art images)
['acorn']
acorn
python3 aas.py
pip3 install redis[hiredis]
python3 aas.py clean
Total time taken in seconds by redis operations: 0.00931716
Total time taken in seconds by non-redis operations: 1.080892801
TOTAL PROGRAM EXECUTION TIME (without user time) == 1.0902099609375
If you run it again: the ascii art options will have been cached in redis and you will see results like this:
Total time taken in seconds by redis operations: 0.006021738
Total time taken in seconds by non-redis operations: 0.30087924
TOTAL PROGRAM EXECUTION TIME (without user time) == 0.3069009780883789
If you then implement the caching of the individual ascii art payloads (uncomment line 139) and you load the same ascii art choice more than once you will see even faster results:
Total time taken in seconds by redis operations: 0.0131917
Total time taken in seconds by non-redis operations: 0.002849102
TOTAL PROGRAM EXECUTION TIME (without user time) == 0.016040802001953125