Common patterns

Using pyappcache to provide caching for requests

Pyappcache provides some added extras to allow it to be used as a cache for the popular requests library when used in conjunction with the CacheControl library.

CacheControl provides the HTTP caching logic and Pyappcache provides the cache backends.

class pyappcache.util.requests.CacheControlProxy(cache)

A proxy to allow Cache instances to be converted for the cachecontrol library’s desired API.

import requests
import cachecontrol
from pyappcache.redis import RedisCache
from pyappcache.util.requests import CacheControlProxy

# Create a Cache instance around Redis
cache = RedisCache()

# Create the proxy, which implements CacheControl's desired API
cc_proxy = CacheControlProxy(cache)

# Create the session
cached_session = cachecontrol.CacheControl(
    requests.Session(),
    cache=cc_proxy
)

# Make the request - first time not cached
cached_session.get("http://calpaterson.com")

# Make the request - seen it before so reads from cache
cached_session.get("http://calpaterson.com")

Storing your cache in a local sqlite file

Sometimes it’s handy to have the cache stored on disk. This is not as fast as in-memory but can be handy if you need a way to persist cache entries but aren’t able to run a “proper” cache server like memcache or redis.

import sqlite3
from pyappcache.sqlite_lru import SqliteCache

sqlite_db = sqlite3.connect("my_cache.sqlite3")
cache = SqliteCache(connection=sqlite_db)