With delete() and delete_many(), you can delete the specific cache values in LocMemCache as shown below. *delele()
and delete_many()
can delete the specific version of single cache value and multiple cache values respectively and my answer explains how to set and get cache values with LocMemCache
and the answer of my question explains the default version of a cache value with LocMemCache
:
from django.http import HttpResponse
from django.core.cache import cache
def test(request):
cache.set("first_name", "John")
cache.set("first_name", "David", version=2)
cache.set("last_name", "Smith")
cache.set("last_name", "Miller", version=2)
cache.set("age", 36)
cache.set("age", 42, version=2)
cache.delete("first_name") # Delete "John"
cache.delete("first_name", 2) # Delete "David"
cache.delete_many(["last_name", "age"]) # Detele "Smith" and 36
cache.delete_many(["last_name", "age"], 2) # Detele "Miller" and 42
return HttpResponse("Test")
In addition, clear() can delete all cache values as shown below:
from django.http import HttpResponse
from django.core.cache import cache
def test(request):
cache.set("first_name", "John")
cache.set("first_name", "David", version=2)
cache.set("last_name", "Smith")
cache.set("last_name", "Miller", version=2)
cache.set("age", 36)
cache.set("age", 42, version=2)
cache.clear() # Delete all
return HttpResponse("Test")