Skip to content

Commit

Permalink
Update to 1.0.19
Browse files Browse the repository at this point in the history
See CHANGELOG.md
  • Loading branch information
awolverp authored Feb 29, 2024
1 parent 6f71ce9 commit 78d6618
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 56 deletions.
23 changes: 13 additions & 10 deletions BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ If you know other library, tell me to add to this page.
> The system on which the benchmarks are done: **Linux x86_64, 8G, Intel i3-1115G4**
## Benchmarks:
We set caches maxsize to 1000.

**Versions**:
- cachebox version: 1.0.0
- cachetools version: 5.3.2

### Cache
| operation\class | cachetools.Cache | cachebox.Cache |
| operation\class | cachetools.Cache(1000) | cachebox.Cache(1000) |
| ----------------------- | ------------------ | ------------------ |
| clear | 0.986ms / 0.728KB | 0.033ms / 0.0KB |
| delete | 0.488ms / 0.144KB | 0.252ms / 0.128KB |
Expand All @@ -24,7 +27,7 @@ We set caches maxsize to 1000.


### FIFOCache
| operation\class | cachetools.FIFOCache | cachebox.FIFOCache |
| operation\class | cachetools.FIFOCache(1000)| cachebox.FIFOCache(1000)|
| ----------------------- | -------------------- | ------------------ |
| clear | 0.909ms / 0.803KB | 0.031ms / 0.0KB |
| delete | 0.602ms / 0.144KB | 0.248ms / 0.128KB |
Expand All @@ -36,7 +39,7 @@ We set caches maxsize to 1000.


### LFUCache
| operation\class | cachetools.LFUCache | cachebox.LFUCache |
| operation\class | cachetools.LFUCache(1000) | cachebox.LFUCache(1000) |
| ----------------------- | -------------------- | ------------------ |
| clear | 28.280ms / 0.898KB | 0.031ms / 0.0KB |
| delete | 1.180ms / 0.216KB | 0.273ms / 0.128KB |
Expand All @@ -48,7 +51,7 @@ We set caches maxsize to 1000.


### LRUCache
| operation\class | cachetools.LRUCache | cachebox.LRUCache |
| operation\class | cachetools.LRUCache(1000) | cachebox.LRUCache(1000) |
| ----------------------- | -------------------- | ------------------ |
| clear | 1.090ms / 0.802KB | 0.030ms / 0.0KB |
| delete | 0.609ms / 0.144KB | 0.268ms / 0.128KB |
Expand All @@ -60,7 +63,7 @@ We set caches maxsize to 1000.


### MRUCache
| operation\class | cachetools.MRUCache | cachebox.MRUCache |
| operation\class | cachetools.MRUCache(1000) | cachebox.MRUCache(1000) |
| ----------------------- | -------------------- | ------------------ |
| clear | 1.105ms / 0.802KB | 0.031ms / 0.0KB |
| delete | 0.630ms / 0.144KB | 0.488ms / 0.128KB |
Expand All @@ -72,7 +75,7 @@ We set caches maxsize to 1000.


### RRCache
| operation\class | cachetools.RRCache | cachebox.RRCache |
| operation\class | cachetools.RRCache(1000) | cachebox.RRCache(1000) |
| ----------------------- | -------------------- | ------------------ |
| clear | 4.879ms / 8.216KB | 0.031ms / 0.0KB |
| delete | 0.480ms / 0.144KB | 0.253ms / 0.128KB |
Expand All @@ -84,7 +87,7 @@ We set caches maxsize to 1000.


### TTLCache
| operation\class | cachetools.TTLCache | cachebox.TTLCache |
| operation\class | cachetools.TTLCache(1000) | cachebox.TTLCache(1000) |
| ----------------------- | -------------------- | ------------------ |
| clear | 4.111ms / 0.882KB | 0.030ms / 0.0KB |
| delete | 1.035ms / 0.144KB | 0.289ms / 0.128KB |
Expand Down Expand Up @@ -118,9 +121,9 @@ git clone https://github.com/awolverp/cachebox
cd cachebox
```

2. Install requirements:
2. Install/Upgrade requirements:
```sh
pip3 install cachetools cachebox
pip3 install -U cachetools cachebox
```

3. Run
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.19]

### Added

- CHANGELOG file added to show you changes

### Fixed

- Improve code stability
- README.md file examples fixed
- Add versions information to BENCHMARK.md file
- `__version__` and `__author__` variables fixed

### Changed

- Makefile test commands changed
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cachebox"
version = "1.0.0"
version = "1.0.19"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ build-prod:
maturin develop --release


.PHONY: test
test:
python3 -m unittest -v
.PHONY: test-py
test-py:
python3 -m unittest



.PHONY: test-rs
test-rs:
cargo clippy
cargo check


.PHONY: test-all
test-all: test-rs test-py


.PHONY: clean
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
**Cachebox** is a Python library (written in Rust) that provides memoizations and cache implementions with
different cache replecement policies.

> [!NOTE]\
> This library is faster than other libraries (between 5x-10x) and uses lower memory than them, [*you can see benchmarks here*](BENCHMARK.md).
This library is faster than other libraries and uses lower memory than them, [*you can see benchmarks here*](BENCHMARK.md).

```python
from cachebox import cached, TTLCache, LRUCache
Expand All @@ -15,23 +14,25 @@ def get_coin_price(coin_name):

# Async functions are supported
@cached(LRUCache(maxsize=126))
async def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
async def get_coin_price(coin_name):
return await async_web3_client.get_price(coin_name)

# You can pass `capacity` parameter.
# If `capacity` specified, the cache will be able to hold at least capacity elements without reallocating.
@cached(LRUCache(maxsize=126, capacity=100))
def get_coin_price(coin_name):
return web3_client.get_price(coin_name)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
```

**Page Content**:
- [Should i use memoization?](#what-is-caching-and-why-to-use-it)
- [Features](#features)
- [Installation](#installation)
- [Tutorial](#tutorial)
- ⁉️ [Should i use memoization?](#what-is-caching-and-why-to-use-it)
- 🎯 [Features](#features)
- 🛠️ [Installation](#installation)
- 🎓 [Tutorial](#tutorial)
- [difference between `TTLCache` and `TTLCacheNoDefault`](#what-is-the-difference-between-ttlcache-and-ttlcachenodefault)
- [Frequently Asked Questions](#frequently-asked-questions)
- ⁉️ [Frequently Asked Questions](#frequently-asked-questions)
- 🆕 [*CHANGELOG*](CHANGELOG.md)
- ⏱️ [*BENCHMARK*](BENCHMARK.md)

### What is caching and why to use it?
Wikipeda:
Expand Down
2 changes: 2 additions & 0 deletions cachebox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
MRUCache as MRUCache,
TTLCacheNoDefault as TTLCacheNoDefault,
TTLCache as TTLCache,
__version__ as __version__,
__author__ as __author__,
)

from .utils import cached as cached
3 changes: 3 additions & 0 deletions cachebox/_cachebox.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import typing
from typing import Iterable

__version__: str
__author__: str

K = typing.TypeVar("K", typing.Hashable)
V = typing.TypeVar("V")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "cachebox"
version = "1.0.0"
version = "1.0.19"
description = "The fastest memoizing and caching Python library written in Rust"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
43 changes: 14 additions & 29 deletions src/ttl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ impl TTLCacheNoDefault {
fn cache_expire(&mut self) {
let mut write = self.inner.write().expect("RwLock is poisoned (write)");

loop {
let key = match self.order.last() {
Some(v) => v,
None => {
break;
}
};

while let Some(key) = self.order.last() {
if !write[key].is_expired() {
break;
}
Expand Down Expand Up @@ -281,7 +274,7 @@ impl TTLCacheNoDefault {
items.0.hash()?,
items.0.into(),
items.1.into(),
expire.clone(),
expire,
)?;
}

Expand Down Expand Up @@ -318,7 +311,7 @@ impl TTLCacheNoDefault {
items.0.hash()?,
items.0.into(),
items.1.into(),
expire.clone(),
expire,
)?;
}

Expand Down Expand Up @@ -541,7 +534,7 @@ impl TTLCacheNoDefault {

let read = self.inner.read().expect("RwLock is poisoned (read)");
if let Some(v) = read.get(&hash) {
return Ok(Some(v.value.clone()));
Ok(Some(v.value.clone()))
} else {
drop(read);

Expand Down Expand Up @@ -572,15 +565,15 @@ impl TTLCacheNoDefault {
Ok(_) => Ok(Some(defaultvalue)),
Err(err) => {
drop(defaultvalue);
return Err(err);
Err(err)
}
}
}
None => match self.cache_setitem(hash, key, defaultvalue.clone(), None) {
Ok(_) => Ok(Some(defaultvalue)),
Err(err) => {
drop(defaultvalue);
return Err(err);
Err(err)
}
},
}
Expand All @@ -596,8 +589,7 @@ impl TTLCacheNoDefault {
) -> PyResult<()> {
let obj = iterable.as_ref(py);

let dur: Option<time::Instant>;
match ttl {
let dur: Option<time::Instant> = match ttl {
Some(seconds) => {
if seconds <= 0.0 {
return Err(pyo3::exceptions::PyValueError::new_err(
Expand All @@ -606,12 +598,12 @@ impl TTLCacheNoDefault {
}

let duration = time::Duration::from_millis((seconds * 1000.0) as u64);
dur = Some(time::Instant::now() + duration);
Some(time::Instant::now() + duration)
}
None => {
dur = None;
None
}
}
};

if obj.is_instance_of::<pyo3::types::PyDict>() {
return self.cache_update_from_pydict(obj.extract()?, dur);
Expand Down Expand Up @@ -819,14 +811,7 @@ impl TTLCache {
fn cache_expire(&mut self) {
let mut write = self.inner.write().expect("RwLock is poisoned (write)");

loop {
let key = match self.order.front() {
Some(v) => v,
None => {
break;
}
};

while let Some(key) = self.order.front() {
if !write[key].is_expired() {
break;
}
Expand Down Expand Up @@ -1196,7 +1181,7 @@ impl TTLCache {

let read = self.inner.read().expect("RwLock is poisoned (read)");
if let Some(v) = read.get(&hash) {
return Ok(Some(v.value.clone()));
Ok(Some(v.value.clone()))
} else {
drop(read);

Expand All @@ -1209,11 +1194,11 @@ impl TTLCache {

match self.cache_setitem(hash, key, defaultvalue.clone()) {
Ok(_) => {
return Ok(Some(defaultvalue));
Ok(Some(defaultvalue))
}
Err(err) => {
drop(defaultvalue);
return Err(err);
Err(err)
}
}
}
Expand Down

0 comments on commit 78d6618

Please sign in to comment.