Giter VIP home page Giter VIP logo

in_memory_cache's Introduction

In-Memory Cache with Eviction Policies

Overview

This Python project implements a thread-safe, in-memory cache with configurable eviction policies. The cache allows storing key-value pairs and supports eviction strategies such as LRU (Least Recently Used), MRU (Most Recently Used), FIFO (First In, First Out), and LIFO (Last In, First Out). It's designed to manage memory efficiently in applications requiring fast access to frequently accessed data.

Features

  • Thread-Safe: Utilizes threading locks (threading.Lock) to ensure safe concurrent access.
  • Eviction Policies: Supports multiple eviction policies:
    • LRU (Least Recently Used)
    • MRU (Most Recently Used)
    • FIFO (First In, First Out)
    • LIFO (Last In, First Out)
  • Expiration: Optional expiration time (TTL) for cached items.
  • Clearing and Deleting: Methods to clear the entire cache or delete specific items.
  • Extensible Design: Easily extendable to support additional eviction policies or customization.

Installation

  1. Clone the Repository:

Fork the repository on GitHub first, then clone your fork

git clone https://github.com/<your-github-username>/in_memory_cache.git
cd in_memory_cache
  1. Create virtual environment:
python -m venv env
  1. Activate virtual environment:
source env/bin/activate
  1. Install Depedencies:
pip install -r requirements.txt
  1. Running Tests
pytest

Usage

Example Code

from cache.base_cache import InMemoryCache
from cache.eviction_policies import EvictionPolicyEnum

# Initialize the cache with LRU eviction policy
cache = InMemoryCache(capacity=100, eviction_policy=EvictionPolicyEnum.LRU)

# Set values in the cache
cache.set('key1', 'value1')
cache.set('key2', 'value2', ttl=60)  # with TTL of 60 seconds

# Retrieve values from the cache
value1 = cache.get('key1')
value2 = cache.get('key2')

# Delete a value from the cache
cache.delete('key1')

# Clear the cache
cache.clear()

Eviction Policies

from cache.eviction_policies import EvictionPolicyEnum

# Example with MRU eviction policy
cache.set_eviction_policy(EvictionPolicyEnum.MRU)

Thread Safety

import threading

# Example using threads
def worker():
    cache.set('key', 'value')

threads = []
for _ in range(10):
    thread = threading.Thread(target=worker)
    threads.append(thread)
    thread.start()

for thread in threads:
    thread.join()

# Ensure cache consistency after threads complete
assert len(cache.cache) <= 3, "Cache should not have more than 3 items."

in_memory_cache's People

Contributors

nikkuag avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.