Skip to content

Latest commit

 

History

History
75 lines (50 loc) · 2.92 KB

README.md

File metadata and controls

75 lines (50 loc) · 2.92 KB

This is a simple example written in python

It showcases Redis acting as a cache for a remote website that serves ascii art

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.

There are two areas you need to edit to get it to work as intended:

line 14 and 17 (make sure you are using proper host and port)

line 158 (uncomment the code here when you are ready to enable caching the individual ascii-art images)

the user will be presented with a busy printout of many ascii art choices

each choice will look like this:

['acorn']

The user should use the text inside the single quotes to indicate their choice eg:

acorn

To run the program you execute:

python3 aas.py

^^ assumes you have redis-py installed

To use redis in your python code - make sure you have this python library installed:

pip3 install redis[hiredis]

You can start the program with no ascii art cached data in Redis by adding an additional argument:

python3 aas.py clean

The first time the program is run you will see results like this:

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

You should see that using the Cache-aside pattern makes your client experience better

The application will also scale much better if you add additional clients -- as they will all share the same cached values.

The ascii art website will be happier too :)