RAM efêmera com TTL, limite de itens e execução paralela de funções
Project description
RAM
Pacote Python para RAM efêmera com TTL, limite de tamanho, execução paralela de funções e import/export em JSON.
Permite criar uma memória temporária, segura para acesso concorrente com threads, com expiração automática, dump/load JSON e execução de código ou funções em paralelo.
Instalação / Installation
pip install ram
Uso em Português / Usage in Portuguese
from ram import RAM import time
Cria a RAM com:
max_size=100 itens
ttl=10 segundos (cada item expira)
max_workers=5 threads paralelas
ram = RAM(max_size=5, ttl=10, max_workers=3)
Armazena um valor
ram.set("chave", "valor") print(ram.get("chave")) # Output: "valor"
Deleta um valor
ram.delete("chave")
Limpa toda a RAM
ram.clear()
Exporta RAM para JSON
json_str = ram.dump_json() print(json_str)
Carrega valores de JSON
ram.load_json(json_str)
Executa funções em paralelo
def exemplo(x): time.sleep(1) ram.set(f"res_{x}", x*2) print("Executado:", x)
for i in range(6): ram.run_parallel(exemplo, i)
Espera todas as tarefas da fila terminarem
ram.tasks.join()
Executa código Python de string
resultado = ram.execute_code("y = 2 + 2") print(resultado["y"]) # Output: 4
Uso em Inglês / Usage in English
from ram import RAM import time
Create RAM with:
max_size=100 items
ttl=10 seconds (each item expires)
max_workers=5 parallel threads
ram = RAM(max_size=5, ttl=10, max_workers=3)
Store a value
ram.set("key", "value") print(ram.get("key")) # Output: "value"
Delete a value
ram.delete("key")
Clear all RAM
ram.clear()
Export RAM to JSON
json_str = ram.dump_json() print(json_str)
Load values from JSON
ram.load_json(json_str)
Run functions in parallel
def example(x): time.sleep(1) ram.set(f"res_{x}", x*2) print("Executed:", x)
for i in range(6): ram.run_parallel(example, i)
Wait for all tasks to finish
ram.tasks.join()
Execute Python code from string
result = ram.execute_code("y = 2 + 2") print(result["y"]) # Output: 4
Funções principais / Main Functions
set(key, value) Armazena um valor na RAM. Se o limite (max_size) for atingido, o item mais antigo é removido. Stores a value in RAM. If the max_size limit is reached, the oldest item is removed.
get(key) Retorna o valor, ou None se não existir ou tiver expirado. Returns the value, or None if it doesn’t exist or has expired.
delete(key) Remove o valor da RAM. Removes a value from RAM.
clear() Limpa toda a memória. Clears all memory.
dump_json() Retorna todos os itens válidos em formato JSON. Returns all valid items as JSON.
load_json(json_str) Carrega valores de um JSON. Loads values from JSON.
run_parallel(func, *args, **kwargs) Adiciona uma função à fila de execução paralela (thread pool). Adds a function to the parallel execution queue (thread pool).
execute_code(code_str, globals_dict=None) Executa código Python passado como string e retorna variáveis locais resultantes. Executes Python code given as a string and returns resulting local variables.
Observações / Notes
max_size define o número máximo de itens na RAM.
ttl é o tempo de vida de cada item (segundos).
max_workers define quantas threads podem executar funções em paralelo.
O acesso à RAM é seguro para uso concorrente com threads.
Valores expirados são removidos automaticamente quando você acessa a RAM ou exporta para JSON.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ram_cache-1.0.0.tar.gz.
File metadata
- Download URL: ram_cache-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b6ff7faaa40638d9f123a48b0badc8cbf982db92c482a2b82a02b89c8e1c878
|
|
| MD5 |
338f6a21bebe3375e29858ecc8a1d856
|
|
| BLAKE2b-256 |
5c5c12b75acbc6ec0db16c2f0a1ac9ae224b9086c4fc53d9e0efb4e3a35a69df
|
File details
Details for the file ram_cache-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ram_cache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f8345d49951db8c56e4e39e315dc85360151c02529a0961bf5a53be15c095bf
|
|
| MD5 |
f21a8cba96973e7a63b07ebe380d45e0
|
|
| BLAKE2b-256 |
d0ced532dc4fcd7c55310327c506af14056596540867636bf708c464fa5ebd83
|