Metadata-Version: 2.4
Name: Sample-test-lib
Version: 0.1.0
Summary: A specialized library developed by Credenti for efficient caching in Flask applications.
Author: Mohan Siddhardha Gude
Author-email: mgude@credenti.com
License: MIT
Classifier: Framework :: Flask
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Intended Audience :: Developers
Description-Content-Type: text/markdown
Requires-Dist: valkey==6.1.1
Requires-Dist: Flask>=3.0.3
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: license
Dynamic: requires-dist
Dynamic: summary

# Ct Cache Lib

A specialized library developed by Credenti for efficient caching in Flask applications.

---
## Features

- Decorator-based caching for Flask endpoints and functions
- Configurable Valkey connection pooling
- Supports cache key templating with nested attribute access
- Easy integration with Flask app lifecycle


---
## Installation

```sh
pip install ct-cache-lib
```

---
## Configuration

Add the following to your Flask app configuration:

```python
app.config['CACHE'] = True              # Enable or disable caching
app.config['CACHE_EXPIRE_TIME'] = 60    # Default cache expiration time in seconds
app.config['CACHE_HOST'] = 'localhost'  # Cache host
app.config['CACHE_PORT'] = 6379         # Cache port
app.config['CACHE_USERNAME'] = 'user'   # Cache username (if required)
app.config['CACHE_PASSWORD'] = 'pass'   # Cache password (if required)
app.config['CACHE_CLIENT_NAME'] = 'my-client' # Cache client name
```
---

## Usage

```python
from flask import Flask
from ct_cache_lib import init_cache
import logging

app = Flask(__name__)
logger = logging.getLogger(__name__)

# Initialize the cache
ct_cache = init_cache(app, logger)

@app.route('/idle_time')
@ct_cache.cache(template_cache_key='idle_time_value:$idle_time', field='$idle_time')
def get_idle_time_value(idle_time: str):
    # Your function logic here
    return {'idle_time': idle_time}
```

---
### Updating a Cache Entry

Update a cache entry directly:

```python
ct_cache.update(
    cache_key='idle_time_value:123',  # Actual cache key
    field='some_field',
    value={'idle_time': 123},
    is_global=False,                  # or True if the key is global
    expire_time=120                   # optional, in seconds
)
```
---
### Deleting a Cache Entry

Delete a cache entry:

```python
ct_cache.delete(
    cache_key='idle_time_value:123',  # Actual cache key, not a template
    field='some_field',
    is_global=False                   # or True if the key is global
)
```
---
### Committing and Resetting Cache

Commit cache writes after each request:

```python
@app.after_request
def after_request(response):
    ct_cache.commit(response)
    return response
```

Reset cache if you need to roll back changes:

```python
ct_cache.reset()
```

---
## Cache Key Specification Examples

You can use dynamic cache keys by referencing function arguments or nested data using `$`.

### Basic Example

```python
@ct_cache.cache(template_cache_key='idle_time_value:$idle_time', field='$idle_time')
def get_idle_time_value(idle_time: str):
    return {'idle_time': idle_time}
```
- If `idle_time = 123`, the cache key will be `idle_time_value:123`.

### Nested Dictionary Example

```python
@ct_cache.cache(template_cache_key='user_cache:$user.id', field='$user.id')
def get_user(user: dict):
    return user
```
- If `user = {'id': 01, 'name': 'User1'}`, the cache key will be `user_cache:01`.

### Nested Object Example

```python
class User:
    def __init__(self, id, name):
        self.id = id
        self.name = name

@ct_cache.cache(template_cache_key='user_cache:$user.id', field='$user.id')
def get_user_obj(user: User):
    return {'id': user.id, 'name': user.name}
```
- If `user.id = 01`, the cache key will be `user_cache:01`.

### Deeply Nested Example

```python
@ct_cache.cache(template_cache_key='deep_cache:$data.profile.info.id', field='$data.profile.info.id')
def get_deep(data: dict):
    return data
```
- If `data = {'profile': {'info': {'id': 01}}}`, the cache key will be `deep_cache:01`.

### Static Key Example

```python
@ct_cache.cache(template_cache_key='static_key')
def get_static():
    return {'result': 1}
```
- The cache key will always be `static_key`.

---
