Skip to content

Commit

Permalink
See CHANGELOG for 3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
awolverp committed Jul 15, 2024
1 parent 40d1248 commit bdeb813
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 89 deletions.
2 changes: 1 addition & 1 deletion APIReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can behave with them same as Python dictionary (only for `VTTLCache` is a li
| FIFOCache | O(1)~ | O(min(i, n-i))* | O(min(i, n-i)) | O(m*min(i, n-i)) | O(1) |
| LFUCache | O(1)~ | O(n)~* | O(1)~ | O(m*n)~ | O(n)~* |
| RRCache | O(1)~ | O(1)~* | O(1)~ | O(m)~ | O(1)~ |
| LRUCache | O(1)~ | ? | O(1)~ | ? | O(1) |
| LRUCache | O(n-k)~| O(1) or O(n-k) | O(n-k)~ | ? | O(1) |
| TTLCache | O(1)~ | O(min(i, n-i))* | O(min(i, n-i)) | O(m*min(i, n-i)) | O(1) |
| VTTLCache | O(1)~ | ? | O(1)~ | ? | O(1)~ |

Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ 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).

## [3.4.0] - 2024-07-15
### Added
- `items_in_order` function added - helps you to iterate caches items in order

### Changed
- `LRUCache` alghoritm changed and optimized (4x faster)
- The `ttl` parameter of `VTTLCache.insert` now has a default value (`None`).
- Testing improved

### Updated
- Dependecies updated

## [3.3.1] - 2024-07-13
### Fixed
- Change comparing alghoritm
Expand Down
37 changes: 23 additions & 14 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cachebox"
version = "3.3.1"
version = "3.4.0"
edition = "2021"

[lib]
Expand All @@ -11,22 +11,20 @@ crate-type = ["cdylib"]
debug = 2
debug-assertions = true
lto = "off"
opt-level = 3

[profile.release]
codegen-units = 1
debug = false
incremental = false
lto = "fat"
opt-level = 3
panic = "abort"
strip = "symbols"

[dependencies]
hashbrown = { version = "^0.14", default-features = false, features=["inline-more", "raw"]}
hashbrown = { version = "^0.14", default-features = false, features=["inline-more", "raw", "allocator-api2"]}
fastrand = "2"
parking_lot = "^0.12"
pyo3 = { version = "^0.21", default-features = false, features=["macros", "extension-module"] }
pyo3 = { version = "^0.22", default-features = false, features=["macros", "extension-module", "py-clone"] }
ahash = { version = "^0.8", default-features = false, features=["std", "compile-time-rng"] }
cfg-if = "1.0"

Expand Down
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ build-prod:


.PHONY: test-py
test-py: build-dev
test-py:
@rm -f cachebox/*.so
maturin develop

python3 -m unittest -v

@rm -f cachebox/*.so
maturin develop --release

python3 -m unittest -v


Expand Down
1 change: 1 addition & 0 deletions cachebox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@
cachedmethod as cachedmethod,
make_hash_key as make_hash_key,
make_typed_key as make_typed_key,
items_in_order as items_in_order,
)
2 changes: 1 addition & 1 deletion cachebox/_cachebox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2074,7 +2074,7 @@ class VTTLCache(BaseCacheImpl[KT, VT]):
"""
...

def insert(self, key: KT, value: VT, ttl: typing.Optional[float]) -> None:
def insert(self, key: KT, value: VT, ttl: typing.Optional[float] = None) -> None:
"""
Inserts a new key-value into the cache.
Expand Down
40 changes: 40 additions & 0 deletions cachebox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,43 @@ def cache_clear():
return functools.update_wrapper(wrapper, func)

return decorator


_K = typing.TypeVar("_K")
_V = typing.TypeVar("_V")

def items_in_order(cache: _cachebox.BaseCacheImpl[_K, _V]) -> typing.Generator[typing.Tuple[_K, _V], typing.Any, None]:
"""
As you know `.items()` methods in implemetations doesn't iter items in order.
You can use this method for iterating items in order.
Supported classes:
- `FIFOCache`
- `LFUCache`
- `LRUCache`
- `TTLCache`
"""
if isinstance(cache, _cachebox.FIFOCache):
for i in range(len(cache)):
key = cache.first(i)
yield key, cache[key]

elif isinstance(cache, _cachebox.LFUCache):
for i in range(len(cache)):
key = cache.least_frequently_used(i)
yield key, cache.peek(key)

elif isinstance(cache, _cachebox.LRUCache):
for i in range(len(cache)):
key = cache.least_recently_used(i)
yield key, cache.peek(key)

elif isinstance(cache, _cachebox.TTLCache):
for i in range(len(cache)):
key = cache.first(i)
yield key, cache[key]

else:
raise TypeError(
"can't iterate items in order for %r class" % type(cache).__name__
)
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 = "3.3.1"
version = "3.4.0"
description = "The fastest memoizing and caching Python library written in Rust"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
4 changes: 2 additions & 2 deletions src/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ macro_rules! hash_object {
#[macro_export]
macro_rules! make_eq_func {
($key:expr) => {
|(x, _)| x.eq(&$key)
|x| x.0.eq(&$key)
};
}

#[macro_export]
macro_rules! make_hasher_func {
() => {
|(x, _)| x.hash
|x| x.0.hash
};
}

Expand Down
Loading

0 comments on commit bdeb813

Please sign in to comment.