File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -511,7 +511,7 @@ def full_hash_key(*args, **kwargs) -> str:
511511#
512512# Encrypted Cache
513513#
514- class EncryptedCache (_KeyMutMapMix ):
514+ class EncryptedCache (_KeyMutMapMix , _StatsMix , MutableMapping ):
515515 """Encrypted Bytes Key-Value Cache.
516516
517517 :param secret: bytes of secret, at least 16 bytes.
@@ -556,6 +556,21 @@ def __getitem__(self, key):
556556 return self ._cipher .new (key = vkey , nonce = vnonce ).decrypt (self ._cache [hkey ])
557557
558558
559+ class StringCache (_KeyMutMapMix , _StatsMix , MutableMapping ):
560+
561+ def __init__ (self , cache ):
562+ self ._cache = cache
563+
564+ def _key (self , key ):
565+ return key .encode ("UTF-8" )
566+
567+ def __setitem__ (self , key , val ):
568+ self ._cache .__setitem__ (self ._key (key ), val .encode ("UTF-8" ))
569+
570+ def __getitem__ (self , key ):
571+ return self ._cache .__getitem__ (key ).decode ("UTF-8" )
572+
573+
559574#
560575# MEMCACHED
561576#
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ with `cachetools`.
4242- ` TwoLevelCache ` allows to combine two caches.
4343- ` DebugCache ` to trace cache calls using ` logging ` .
4444- ` EncryptedCache ` a cache with key hashing and value encryption.
45+ - ` StringCache ` maps strings to bytes.
4546
4647### Cache utilities
4748
Original file line number Diff line number Diff line change @@ -547,9 +547,15 @@ def test_cache_key():
547547
548548def test_encrypted_cache ():
549549 SECRET = b"incredible secret key for testing encrypted cache..."
550+ # bytes
550551 cache = ctu .EncryptedCache (ctu .DictCache (), SECRET )
551552 cache [b"Hello" ] = b"World!"
552553 assert b"Hello" in cache
553554 assert cache [b"Hello" ] == b"World!"
554555 del cache [b"Hello" ]
555556 assert b"Hello" not in cache
557+ scache = ctu .StringCache (cache )
558+ scache ["Hello" ] = "World!"
559+ assert "Hello" in scache
560+ assert scache ["Hello" ] == "World!"
561+ del scache ["Hello" ]
You can’t perform that action at this time.
0 commit comments