Giter Site home page Giter Site logo

ttldictionary's Introduction

Time-to-live python dictionary

This repository contains the implementation of a simple dictionary with Time-To-Live (TTL) functionality in Python. This code was created as a part of side project.

Overview

The TTLDictionary is a dictionary-like object where each key-value pair has a time-to-live (TTL). The key-value pairs are automatically removed from the dictionary when their TTL expires. This is achieved by using a daemon thread that continuously checks for expired key-value pairs and removes them.

Features

  • Thread-safe operations: The TTLDictionary uses a threading.RLock() to ensure thread-safe operations.
  • Automatic cleanup: A daemon thread is used to continuously clean up expired key-value pairs.
  • Flexible TTL: Each key-value pair can have a different TTL. If no TTL is provided, the key-value pair will not expire.
  • The repository also includes a set of unit tests (ttl_dictionary_ut.py) to verify the functionality of the TTLDictionary

Example of Usage

from ttl_dictionary import TTLDictionary
import time

# Create a new TTLDictionary
ttl_dict = TTLDictionary()

# Add a key-value pair with a TTL of 5 seconds
ttl_dict["key1"] = ("value1", 5)

# Add a key-value pair with no TTL (it will not expire)
ttl_dict["key2"] = "value2"

# Get a value
print(ttl_dict["key1"])  # Outputs: value1

# Wait for 6 seconds
time.sleep(6)

# Try to get the value of "key1" again
try:
    print(ttl_dict["key1"])
except KeyError:
    print("Key1 is not in the dictionary")  # Outputs: Key1 is not in the dictionary

# Check if "key2" is in the dictionary
if "key2" in ttl_dict:
    print("Key2 is in the dictionary")  # Outputs: Key2 is in the dictionary

# Get the number of key-value pairs in the dictionary
print(len(ttl_dict))  # Outputs: 1

License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

CC BY-SA 4.0

ttldictionary's People

Contributors

gdahuks 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.