Giter VIP home page Giter VIP logo

script.module.cache's Introduction

script.module.cache

foo

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.

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

Examples

Quick examples of basic usage

Request cache

How to use the Cache class with requests

The Cache class allows you to easily store URIs, as keys, along with their response data and headers in a persistent database.

The calculated column "fresh" and the "conditional_headers" method can be used to limit the number of requests made and the amount of data transferred respectively.

import requests
from cache import Cache, conditional_headers
 
  
def get_html(uri):
    # type: (str) -> Union[str, None]
    headers = {
        "Accept": "text/html",
        "Accept-encoding": "gzip"
    }
    with Cache() as c:
        # check if uri is cached
        cached = c.get(uri)
        if cached:
            # if the data is fresh then simply return it
            if cached["fresh"]:
                return cached["blob"]
            # otherwise append applicable "If-None-Match"/"If-Modified-Since" headers
            headers.update(conditional_headers(cached))
        # request a new version of the data
        r = requests.get(uri, headers=headers, timeout=60)
        if 200 == r.status_code:
            # add the new data and headers to the cache 
            c.set(uri, r.content, r.headers)
            return r.content
        if 304 == r.status_code:
            # the data hasn't been modified so just touch the cache with the new headers
            # and return the existing data
            c.touch(uri, r.headers)
            return cached["blob"]
        # perhaps log any other status codes for debugging
        return None
 

Generic string storage

Generic string storage is facilitated by the Store class.

The Store class allows a "key" to be used to append to, remove from or retrieve a set of unique string values.

This is useful for saving things like; a history of searched terms, urls of played films, etc.

from cache import Store
 
 
# key can be anything, app:// prefix isn't required
# the only requirement is that the key be unique with in your add-on
searches = Store("app://user-searches") 
 
# add strings to the store
searches.append("foo")
searches.append("bar")
searches.append("bat")
print(searches.retrieve())  # {'bar', 'bat', 'foo'}
 
# remove a string from the store
searches.remove("bar")  
print(searches.retrieve())  # {'bat', 'foo'}
  
# clear the store
searches.clear()
print(searches.retrieve())  # set()
 

References

Codacy Badge

script.module.cache's People

Contributors

fraserchapman avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

script.module.cache's Issues

Matrix Support

Hi,
is there any chance, you will update your plugin to support Kodi Matrix / Python 3?
I'm using your module for token caching etc in my plugin, so if you are not currently planning an supporting Matrix, I need to switch to Kodi default Cache management (which in this context is a pain ๐Ÿ˜‰)

Thanks,
Regards

bbsan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.