caching

Python functools lru_cache with instance methods: release object

Python functools lru_cache with instance methods: release object Question: How can I use functools.lru_cache inside classes without leaking memory? In the following minimal example the foo instance won’t be released although going out of scope and having no referrer (other than the lru_cache). from functools import lru_cache class BigClass: pass class Foo: def __init__(self): self.big …

Total answers: 9

Python in-memory cache with time to live

Python in-memory cache with time to live Question: I have multiple threads running the same process that need to be able to to notify each other that something should not be worked on for the next n seconds its not the end of the world if they do however. My aim is to be able …

Total answers: 12

Make @lru_cache ignore some of the function arguments

Make @lru_cache ignore some of the function arguments Question: How can I make @functools.lru_cache decorator ignore some of the function arguments with regard to caching key? For example, I have a function that looks like this: def find_object(db_handle, query): # (omitted code) return result If I apply lru_cache decorator just like that, db_handle will be …

Total answers: 2

Disable cache on a specific page using Flask

Disable cache on a specific page using Flask Question: I have a template showing various entries that the author can edit/delete. Users can delete their posts clicking on Delete After the deletion the user gets redirected to the entries page, but the item is still there, and the page needs to be reloaded again to …

Total answers: 3

Most straightforward way to cache geocoding data

Most straightforward way to cache geocoding data Question: I am using geopy to get lat/long coordinates for a list of addresses. All the documentation points to limiting server queries by caching (many questions here, in fact), but few actually give practical solutions. What is the best way to accomplish this? This is for a self-contained …

Total answers: 5

python django mock cache

python django mock cache Question: In my settings.py , I have specified my cache as : CACHES = { ‘default’: { …… } } In my views.py, I have import requests from django.core.cache import cache, get_cache def aview(): #check cache if not get_cache(‘default’).get(‘key’): #make request and save in cache result = request.get(‘some_url’) get_cache(‘default’).set(‘key’, result) return …

Total answers: 1

PyPI is slow. How do I run my own server?

PyPI is slow. How do I run my own server? Question: When a new developer joins the team, or Jenkins runs a complete build, I need to create a fresh virtualenv. I often find that setting up a virtualenv with Pip and a large number (more than 10) of requirements takes a very long time …

Total answers: 5

What's with the integer cache maintained by the interpreter?

What's with the integer cache maintained by the interpreter? Question: After dive into Python’s source code, I find out that it maintains an array of PyInt_Objects ranging from int(-5) to int(256) (@src/Objects/intobject.c) A little experiment proves it: >>> a = 1 >>> b = 1 >>> a is b True >>> a = 257 >>> …

Total answers: 1

Python LRU Cache Decorator Per Instance

Python LRU Cache Decorator Per Instance Question: Using the LRU Cache decorator found here: http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/ from lru_cache import lru_cache class Test: @lru_cache(maxsize=16) def cached_method(self, x): return x + 5 I can create a decorated class method with this but it ends up creating a global cache that applies to all instances of class Test. However, …

Total answers: 3

Performance of Redis vs Disk in caching application

Performance of Redis vs Disk in caching application Question: I wanted to create a redis cache in python, and as any self respecting scientist I made a bench mark to test the performance. Interestingly, redis did not fare so well. Either Python is doing something magic (storing the file) or my version of redis is …

Total answers: 1