DEV Community

Cover image for How to implement a cache manager with the singleton pattern using Python
Mustafa Elghrib
Mustafa Elghrib

Posted on • Updated on • Originally published at mustafaelghrib.github.io

How to implement a cache manager with the singleton pattern using Python

Introduction

In this article we will build a cache manager that stores frequently used data in memory using the Singleton pattern where the Singleton instance can ensure that the data is accessed efficiently and that memory usage is optimized.

UML Diagram of the Cache Manager

UML Diagram of the Cache Manager

Code Implementation

The cache manager is very simple approach, we need to save data with predefined key and then use this key to retrieve this data and this approach could be achieved through the set and get methods as shown below, we will also use the singleton pattern that require a static method and private constructor, so we could get only one instance from the class every time we call it



class CacheManager(object):

    _instance = None
    _cache_store = []

    def __init__(self):
        raise RuntimeError("Call get_instance() instead")

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls.__new__(cls)
        return cls._instance

    def set(self, key, value):
        self._cache_store.append((key, value))

    def get(self, key):
        value = None
        for item in self._cache_store:
            if key in item:
                value = item[1]
        return value


Enter fullscreen mode Exit fullscreen mode

This is just a simple example of cache manager, where it has two methods for setting and getting cache values.

Testing the Cache Manager

We could test the cache manager as follows:



cache1 = CacheManager.get_instance()
cache1.set("name", "Mustafa")

cache2 = CacheManager.get_instance()
cache2.set("role", "backend engineer")

print(cache2.get("name")) # note we use cache2 to get the name
print(cache2.get("role"))


Enter fullscreen mode Exit fullscreen mode

And here is the result:



Mustafa
backend engineer


Enter fullscreen mode Exit fullscreen mode

As you can see, even creating multiple instances from the cache manager, but we still access only one instance.

Conclusion

This cache manager could be useful in any Python application, where it caches data in memory and optimize the application performance.

References

Top comments (0)