Q:

How to clear a cached query?

I'm using the following method to cache the result of a SQL query:
db(my_query).select(cache=(cache.ram, 3600), cacheable=True)

In some cases I want to clear this cache before it expires, which could be done by using cache.ram.clear(key) but I actually don't know the key generated by the DAL in the previous code.
I understand cache=(cache.ram, 0) will clear the cache too, but I also have the overhead of executing the query.
How can I achieve this?

A:

The cache key is a bit complicated to replicate (it is the MD5 hash of the database URI plus the SQL generated for the query). As an alternative, since you have cacheable=True, you can call cache.ram directly with your own key:
rows = cache.ram('my_query', lambda: db(my_query).select(cacheable=True), 3600)

