Login

Database Cache Management View

Author:
kedare
Posted:
December 8, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

A simple view used to manage the page cache stored in the database (here Postgresql in the django_cache table, you also have to set correctly CACHE_TABLE_OID, by the OID of the cache table (you can get it in PgAdmin)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from django.contrib.admin.views.decorators import staff_member_required
from django.utils.translation import ugettext_lazy as _
from django.db import connection

CACHE_TABLE_OID = 69126

@staff_member_required
def admin_cache_management(request):
    cursor = connection.cursor()
    message = ""
    if request.POST:
    	cursor.execute("DELETE FROM django_cache")
    	cursor.execute("COMMIT")
    	cursor.execute("VACUUM FULL FREEZE ANALYZE django_cache")
    	message = _("A manual cache flush has been requested by an administrator, and was executed with success")
    	
    cursor.execute("""
    SELECT 
		pg_stat_get_numscans(%s),
		pg_stat_get_tuples_updated(%s),
		pg_stat_get_tuples_inserted(%s),
		pg_stat_get_tuples_deleted(%s),
		COUNT(*),
		pg_size_pretty(pg_total_relation_size('django_cache'))
	FROM django_cache;
    """, (CACHE_TABLE_OID, CACHE_TABLE_OID, CACHE_TABLE_OID, CACHE_TABLE_OID));
    data = cursor.fetchone()
    cache_hits = int(data[0])
    cache_updated = int(data[1])
    cache_inserted = int(data[2])
    cache_deleted = int(data[3])
    cache_entry_amount = int(data[4])
    cache_size = data[5]

    cursor.execute("COMMIT")
    return render_to_response("system/admin_cache_management.html", 
    {
    	"cache_entry_amount": cache_entry_amount,
    	"cache_size": cache_size,
    	"cache_hits": cache_hits,
    	"cache_updated": cache_updated,
    	"cache_inserted": cache_inserted,
    	"cache_deleted": cache_deleted,
    	"message": message,
    })

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.