A class for caching in a session.
Project description
Session Cache
Description
Session Cache is a Python package that provides a simple and efficient way to store and manage cached data for a session. It allows users to cache data during a session and automatically clean up expired data at regular intervals.
Utility
Session Cache is useful for scenarios where temporary data storage is required within a session, such as web applications, API caching, or any situation where quick access to cached data is needed.
Install
You can install the session-cache
package via pip:
pip install ran-cache
Examples of Basic Uses
from scache import SessionCache as Cache
from time import sleep
# Create a session cache with default settings (ttl=1 hour, cleanup_interval=2.5 hours)
# If 'cleanup_config' is an integer > 0, automatic cleanup is started automatically
cache = Cache() # default_ttl=3600, cleanup_config=9000
# Add data to the cache with default_ttl
cache.update('default_ttl', cache.default_ttl)
# Add data to the cache with custom ttl
cache.update('custom_ttl', 'custom', ttl=4) # 4 seconds
# Add data to the cache without ttl
cache.update('persistent_data', 'persistent', ttl=None)
# Retrieve cached values
default_ttl = cache.get("default_ttl")
custom_ttl = cache.get("custom_ttl")
print(default_ttl) # Returns 3600
print(custom_ttl) # Returns "custom"
sleep(4)
# Check cached values after expiration
default_ttl = cache.get("default_ttl")
custom_ttl = cache.get("custom_ttl")
print(default_ttl) # Returns 3600
print(custom_ttl) # Returns None
# Change default_ttl
cache.default_ttl = 2
# Check if automatic cleanup is enabled
print(cache.cleaning_enabled) # Returns True
# Stop automatic cleanup
cache.stop_cleanup()
print(cache.cleaning_enabled) # Returns False
# Start automatic cleanup
cache.start_cleanup()
print(cache.cleaning_enabled) # Returns True
# Automatic cleanup interval
print(cache.cleanup_config) # Returns 9000
# Change automatic cleanup interval to 1 minute
cache.cleanup_config = 60
print(cache.cleanup_config) # Returns 60
# Update a cached value and apply Default TTL (2 seconds)
cache.update("default_ttl", "new_value")
print(cache.get("default_ttl")) # Returns "new_value"
sleep(2)
print(cache.get("default_ttl")) # Returns None
Contact
For any inquiries or feedback regarding this project, please contact us on Discord
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ran-cache-0.1.0.tar.gz
(5.2 kB
view hashes)
Built Distribution
Close
Hashes for ran_cache-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a80f5c39d2055ef27ff89f36dfb0316bfa340e5304c21ee2d7c55cab3f160148 |
|
MD5 | 31c3f07afad09f28b150a7e007e88f9b |
|
BLAKE2b-256 | c14ad7dac61ed5de2841576e98c9150056a21feb04fff4b6bd9d040323395e1a |