Cache Helper Utilities

A snippet adding wrapper helpers around Django cache API.


# models.py
from django.db import models
class Counter(models.Model):
    name = models.CharField(max_length=50, unique=True)
    value = models.IntegerField(default=0)
  

# admin.py
from django.contrib import admin
from .models import Counter
@admin.register(Counter)
class CounterAdmin(admin.ModelAdmin):
    list_display = ("name","value")
  

from django.core.cache import cache
def cache_get(key, default=None): return cache.get(key, default)
def cache_set(key, value, timeout=300): cache.set(key, value, timeout); return value
def cache_del(key): cache.delete(key)
  
Explanation:
  • Wrap Django cache API to standardize timeouts and naming.
  • Use per-view or low-level cache depending on the use case.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.