Caching¶
The caching module provides flexible caching mechanisms to improve performance and reduce API calls.
Caching Module for BingX API.
This module provides synchronous and asynchronous caching implementations for the BingX API. It includes in-memory and Redis-based caches, with support for time-to-live (TTL) and thread-safe operations.
- Classes:
BaseCache: Abstract base class for synchronous caches.
BaseAsyncCache: Abstract base class for asynchronous caches.
SyncMemoryCache: Synchronous in-memory cache implementation.
AsyncMemoryCache: Asynchronous in-memory cache implementation.
SyncRedisCache: Synchronous Redis cache implementation.
AsyncRedisCache: Asynchronous Redis cache implementation.
from bingx_py.caching import SyncMemoryCache, AsyncMemoryCache
# Synchronous in-memory cache
sync_cache = SyncMemoryCache()
sync_cache.set("key", {"data": "value"}, ttl=60)
data = sync_cache.get("key")
# Asynchronous in-memory cache
async_cache = AsyncMemoryCache()
await async_cache.aset("key", {"data": "value"}, ttl=60)
data = await async_cache.aget("key")
- class bingx_py.caching.AsyncMemoryCache[source]¶
Bases:
BaseAsyncCacheAsynchronous in-memory cache implementation.
This class provides an async-safe in-memory cache implementation. It provides methods to set and get data with optional TTL (time-to-live) support.
- class bingx_py.caching.AsyncRedisCache(host='localhost', port=6379, db=0)[source]¶
Bases:
BaseAsyncCacheAsynchronous Redis cache implementation.
This class provides an asynchronous Redis cache implementation using the redis.asyncio library. It provides methods to set and get data from Redis, with optional TTL (time-to-live) support.
- Parameters:
host (str)
port (int)
db (int)
- async aget(key)[source]¶
Get data from Redis asynchronously.
- Parameters:
key (str) – The key to retrieve from Redis.
- Returns:
The retrieved data, or None if the key does not exist.
- Return type:
Optional[dict[str, Any]]
- async aset(key, value, ttl=None)[source]¶
Set data in Redis asynchronously.
- Parameters:
key (str) – The key to set in Redis.
value (dict[str, Any]) – The data to set.
ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.
- Returns:
None
- Return type:
None
- class bingx_py.caching.BaseAsyncCache[source]¶
Bases:
ABCBase class for asynchronous caches.
This class provides a basic interface for asynchronous caches. It provides methods to set and get data from the cache, with optional TTL (time-to-live) support.
- abstract async aget(key)[source]¶
Get data from the cache asynchronously.
- Parameters:
key (str) – The key to retrieve from the cache.
- Returns:
The retrieved data, or None if the key does not exist.
- Return type:
Optional[dict[str, Any]]
- abstract async aset(key, value, ttl=None)[source]¶
Set data in the cache asynchronously.
- Parameters:
key (str) – The key to set in the cache.
value (dict[str, Any]) – The data to set.
ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.
- Returns:
None
- Return type:
None
- class bingx_py.caching.BaseCache[source]¶
Bases:
ABCBase class for synchronous caches.
This class provides a basic interface for synchronous caches. It provides methods to set and get data from the cache, with optional TTL (time-to-live) support.
- abstract get(key)[source]¶
Get data from the cache.
- Parameters:
key (str) – The key to retrieve from the cache.
- Returns:
The retrieved data, or None if the key does not exist.
- Return type:
Optional[dict[str, Any]]
- abstract set(key, value, ttl=None)[source]¶
Set data in the cache.
- Parameters:
key (str) – The key to set in the cache.
value (dict[str, Any]) – The data to set.
ttl (Optional[int]) – The time-to-live (TTL) for the data in seconds. If not provided, the data will not expire.
- Returns:
None
- Return type:
None
- class bingx_py.caching.SyncMemoryCache[source]¶
Bases:
BaseCacheSynchronous in-memory cache implementation.
This class provides a thread-safe synchronous in-memory cache implementation. It provides methods to set and get data with optional TTL (time-to-live) support.
- class bingx_py.caching.SyncRedisCache(host='localhost', port=6379, db=0)[source]¶
Bases:
BaseCacheSynchronous Redis cache implementation.
This class provides a synchronous Redis cache implementation using the redis library. It provides methods to set and get data from Redis, with optional TTL (time-to-live) support.
- Parameters:
host (str)
port (int)
db (int)