Skip to content

Commit

Permalink
Small bug-fix with Store class retrieve
Browse files Browse the repository at this point in the history
  • Loading branch information
FraserChapman committed Jul 14, 2019
1 parent d3c935f commit d4397d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

Simple Python HTTP Cache using sqlite3

* Stores cached data in sqlite3.Blobs.
* Calculates lifetime, freshness as virtual columns.
* Obeys cache control directives, immutable, no-store, etc.
* Supports etag, last_modified, etc for conditional requests.
* Can be used as a generic key/blob data store when used without directives.
* Stores cached data in sqlite3.Blobs.
* Calculates lifetime, freshness as virtual columns.
* Obeys cache control directives, immutable, no-store, etc.
* Supports etag, last_modified, etc for conditional requests.
* Can be used as a generic key/blob data store when used without directives.

By default the Cache (and Store) use a sqlite3 database named "cache.sqlite" located in the add-on profile directory

# Examples
## Examples

Quick examples of basic usage

Expand Down Expand Up @@ -93,8 +93,10 @@ print(searches.retrieve()) # set()

~~~~

# References
## References

* [DB-API 2.0 interface for SQLite databases](https://docs.python.org/2/library/sqlite3.html)
* [RFC7234 - HTTP/1.1 Caching](https://tools.ietf.org/html/rfc7234)
* [RFC7232 - HTTP/1.1 Conditional Requests](https://tools.ietf.org/html/rfc7232)
* [DB-API 2.0 interface for SQLite databases](https://docs.python.org/2/library/sqlite3.html)
* [RFC7234 - HTTP/1.1 Caching](https://tools.ietf.org/html/rfc7234)
* [RFC7232 - HTTP/1.1 Conditional Requests](https://tools.ietf.org/html/rfc7232)

[![Codacy Badge](https://api.codacy.com/project/badge/Grade/26afc2828d1e439eafd563967a391f5b)](https://www.codacy.com/app/FraserChapman/script.module.cache?utm_source=github.com&utm_medium=referral&utm_content=FraserChapman/script.module.cache&utm_campaign=Badge_Grade)
11 changes: 5 additions & 6 deletions addon.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.module.cache" name="cache" version="1.0.0" provider-name="fraser">
<addon id="script.module.cache" name="cache" version="1.0.1" provider-name="fraser">
<requires>
<import addon="xbmc.python" version="2.26.0"/>
</requires>
<extension point="xbmc.python.module" library="lib"/>
<extension point="xbmc.addon.metadata">
<summary lang="en_GB">Simple HTTP Cache using sqlite3</summary>
<description lang="en_GB">Stores cached data in sqlite3.Blobs. Calculates lifetime, freshness as virtual
columns.
Obeys cache control directives, immutable, no-store, etc. Supports etag, last_modified, etc for validation.
Can be used as a generic key/blob data store when used without directives
<description lang="en_GB">Stores cached data in sqlite3.Blobs. Calculates lifetime, freshness as virtual columns.
Obeys cache control directives, immutable, no-store, etc. Supports etag, last_modified, etc for validation.
Can be used as a generic key/blob data store when used without directives
</description>
<license>MIT</license>
<platform>all</platform>
<email>fraser.chapman@gmail.com</email>
<source>https://github.com/FraserChapman/script.module.cache</source>
<news>v1.0.0 (17-6-19) - Initial version</news>
<news>v1.0.1 (6-7-19) - Small bug-fix with Store class retrieve</news>
</extension>
</addon>
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
v1.0.1 - Small bug-fix with Store class retrieve
v1.0.0 - Initial version
12 changes: 6 additions & 6 deletions lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def httpdate_to_datetime(input_date):


def datetime_to_httpdate(input_date):
# type: (datetime) -> Optional[datetime]
# type: (datetime) -> Optional[str]
"""Converts datetime to http date string"""
if input_date is None:
return None
Expand All @@ -55,7 +55,7 @@ def datetime_to_httpdate(input_date):

def conditional_headers(row):
# type: (sqlite3.Row) -> dict
"""Creates dict of If-None-Match and If-Modified-Since headers based on row etag and last_modified columns"""
"""Creates dict of conditional request headers based on row etag and last_modified"""
headers = {}
if row["etag"] is not None:
headers["If-None-Match"] = row["etag"]
Expand Down Expand Up @@ -86,7 +86,7 @@ def retrieve(self):
"""Gets set of stored strings"""
with Cache(self.db) as c:
data = c.get(self.key)
return data["blob"] if data is set else set()
return data["blob"] if data else set()

def _save(self, data):
# type: (set) -> None
Expand Down Expand Up @@ -167,13 +167,13 @@ def __init__(self, db=None):
def get(self, uri):
# type: (str) -> Optional[sqlite3.Row]
"""Retrieve a partial entry from the cache"""
query = """SELECT blob, last_modified, etag, immutable,
query = """SELECT blob, last_modified, etag, immutable,
CASE
WHEN max_age THEN max(age, max_age)
ELSE CASE
WHEN expires THEN expires - http_date
ELSE cast((datetime('now') - last_modified) / 10 as int)
END
END
END >= strftime('%s', datetime('now')) - strftime('%s', http_date) AS fresh
FROM data WHERE uri=?"""
result = self._execute(query, (uri,))
Expand All @@ -191,7 +191,7 @@ def set(self, uri, content, headers=None):
# never store items marked "no-store"
if "no-store" in directives:
return
query = """REPLACE INTO data (uri, blob, http_date, age, etag, expires, last_modified, max_age, immutable)
query = """REPLACE INTO data (uri, blob, http_date, age, etag, expires, last_modified, max_age, immutable)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"""
values = (
uri,
Expand Down

0 comments on commit d4397d7

Please sign in to comment.