Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redisign the cahce API #240

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
38b624c
Remove the old cache implementations
hexagonrecursion Feb 16, 2021
95d24f8
Create a draft of the new cache API
hexagonrecursion Feb 16, 2021
3047387
Add method commit() to WriteHandle
hexagonrecursion Feb 16, 2021
bfb96fd
Add a comment
hexagonrecursion Feb 16, 2021
507bfb0
Fix import failing
hexagonrecursion Feb 18, 2021
3d30237
TDD
hexagonrecursion Feb 18, 2021
d166fa6
More TDD
hexagonrecursion Feb 18, 2021
f75ea1b
Refactor tests
hexagonrecursion Feb 18, 2021
ca901a9
Improve a test
hexagonrecursion Feb 18, 2021
1b4aea2
Delete docstring
hexagonrecursion Feb 18, 2021
f4b0930
More TDD
hexagonrecursion Feb 18, 2021
ac77d1d
More TDD
hexagonrecursion Feb 18, 2021
86b4a81
More TDD
hexagonrecursion Feb 19, 2021
49e2696
Break code on purpose
hexagonrecursion Feb 20, 2021
31f7e4b
And fix it
hexagonrecursion Feb 20, 2021
b3606c2
Break code again
hexagonrecursion Feb 20, 2021
e759b29
And fix it
hexagonrecursion Feb 20, 2021
e8c2311
Line too long
hexagonrecursion Feb 20, 2021
9ecfdb3
TDD
hexagonrecursion Feb 20, 2021
a128591
TDD
hexagonrecursion Feb 20, 2021
31363c9
TDD
hexagonrecursion Feb 20, 2021
bc47bc6
TDD
hexagonrecursion Feb 20, 2021
fe25d60
Add comment
hexagonrecursion Feb 20, 2021
013089b
Drop commented out code
hexagonrecursion Feb 20, 2021
8398e32
Break code
hexagonrecursion Feb 20, 2021
13b565c
And fix it
hexagonrecursion Feb 20, 2021
1d475f6
Break code
hexagonrecursion Feb 20, 2021
852dd3c
And fix it
hexagonrecursion Feb 20, 2021
3a2e98b
Add a docstring
hexagonrecursion Feb 20, 2021
23163e4
TDD
hexagonrecursion Feb 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cachecontrol/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from requests.adapters import HTTPAdapter

from .controller import CacheController, PERMANENT_REDIRECT_STATUSES
from .cache import DictCache
# from .cache import DictCache
from .filewrapper import CallbackFileWrapper


Expand Down
43 changes: 0 additions & 43 deletions cachecontrol/cache.py

This file was deleted.

6 changes: 0 additions & 6 deletions cachecontrol/caches/__init__.py

This file was deleted.

150 changes: 0 additions & 150 deletions cachecontrol/caches/file_cache.py

This file was deleted.

37 changes: 0 additions & 37 deletions cachecontrol/caches/redis_cache.py

This file was deleted.

2 changes: 1 addition & 1 deletion cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from requests.structures import CaseInsensitiveDict

from .cache import DictCache
#from .cache import DictCache
from .serialize import Serializer


Expand Down
103 changes: 103 additions & 0 deletions cachecontrol/streaming_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# SPDX-License-Identifier: Apache-2.0

import io


class NotFound(Exception):
'''Raised by Cache.open_read() and Cache.delete() when the key is missing'''


class ReadHandle:
def read(self, size: int = -1) -> bytes:
pass

def close(self) -> None:
pass


class WriteHandle:
def write(self, b: bytes) -> int:
pass

def commit(self) -> None:
'''Assign the value to the key and close the WriteHandle'''
pass

def close(self) -> None:
'''Discard the value without assigning it to the key'''
pass


class Cache:
'''Cache API docs

No need to inherit form this. This is just a documenation
of the interface between CacheControl and a cache
'''

# TODO: are keys really str?
def open_read(self, key: str) -> ReadHandle:
'''Get data from the cache

Throw NotFound if the key is missing
'''
pass

def open_write(self, key: str, expires=None) -> WriteHandle:
pass

def delete(self, key):
'''Delete key from the cache

Throw NotFound if the key is missing
'''
pass

def close(self):
'''Cleanup any temporary files, database connections etc.'''
pass
# Cache does not have __enter__ and __exit__ on purpose. Same for ReadHandle
# and WriteHandle. The idea is to keep the interface small, simple and easy
# to implement - it is the interface between CacheControl and storage
# backends. Helpers can be written on top of this interface
# to make it easier to use.


# An example to use while prototyping
class ExampleCache:
class WriteHandle:
def __init__(self, put):
self.put = put
self.buf = io.BytesIO()

def write(self, b):
self.buf.write(b)

def commit(self):
self.put(self.buf.getvalue())

def close(self):
pass

def __init__(self):
self.data = {}

def open_read(self, key):
try:
return io.BytesIO(self.data[key])
except KeyError:
raise NotFound(f'{key} not found in the cache')

def open_write(self, key, expires=None):
def put(b):
self.data[key] = b
return self.WriteHandle(put)

def delete(self, key):
try:
del self.data[key]
except KeyError:
raise NotFound(f'{key} not found in the cache')

def close(self):
pass
2 changes: 1 addition & 1 deletion cachecontrol/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

from .adapter import CacheControlAdapter
from .cache import DictCache
# from .cache import DictCache


def CacheControl(
Expand Down
Loading