memory-cache

It is a package that simply stores and uses a cache in memory.


License
MIT
Install
pip install memory-cache==1.0.0

Documentation

Project logo

Memory Cache

Status GitHub Issues GitHub Pull Requests License


It is a package that simply stores and uses a cache in memory.

📝 Table of Contents

🧐 About

You can use following functions.

🏁 Getting Started

Installing

pip install memory_cache

Tutorial

Execute the code below.

import memory_cache
import time
import random

cache = memory_cache.MemoryCache()

cache_time = 10
working_time = 4
while True:
    print("\n=== Press any key to get started. ===")
    input()

    key = "fruit"
    data = cache.get(key)

    if data == None:
        print(f"working for {working_time} seconds ...")

        time.sleep(working_time)

        data = {"name": "apple", "price": "120"}
        cache.put(key, data, cache_time=cache_time)

        print("working complete!")
    else:
        print("cached!")

    print(data)

Execution Result:

=== Press any key to get started. ===

working for 3 seconds ...
working complete!
{'name': 'apple', 'price': '120'}

=== Press any key to get started. ===

cached!
{'name': 'apple', 'price': '120'}

=== Press any key to get started. ===

cached!
{'name': 'apple', 'price': '120'}

...

If open saved data, You can see that it is saved in the format below.

{
    "value": {
        "name": "apple",
        "price": "120"
    },
    "cache_time": 10,
    "put_time": 1596727712.0505128
}

🎈 Usage

Please check Prerequisites before starting Usage.

🌱 put

Parameters

  • key: str

    It is a key for storing data and finding data.

  • value: any type

    The data to be saved.

  • cache_time: int (default: -1)

    Cache time. If this value is -1, it is not cached

🌱 get

Parameters

  • key: str

    It is a key for storing data and finding data.

🎉 Acknowledgements

  • Title icon made by Freepik.

  • If you have a problem. please make issue.

  • Please help develop this project 😀

  • Thanks for reading 😄