Skip to main content

Django application that keeps content visit count in redis first, then dump to database via extra job.

Project description

Django application that keeps content visit count in redis first, then dump to database via extra job.

Install

pip install django-redis-counter

Dependencies

  • django

  • redis

Usage

  1. Add drc in INSTALLED_APPS in django settings.py. drc is short for django-redis-counter.

INSTALLED_APPS = [
    ...
    'drc',
    ...
]
  1. Define counter models in app’s models.py.

from drc.models import Counter

class Page(models.Model):
    title = models.CharField(max_length=32)
    ...

class PageCounter(Counter):
    pass
  1. Call incr in views.

def display_page(request, page_id):
    page = Page.objects.get(pk=page_id)
    page_visit_number = PageCounter.incr(page)
    return render(request, "page.html", {
        "page": page,
        "page_visit_number": page_visit_number,
    })
  1. Create a script to dump cached data to database. Name the script to page_counter_dump.sh or what ever you like.

#!/bin/bash
cd /your/project/path
python manage.py rdc-dump
  1. Add dump task as schedule job, e.g. crontab job.

* * * * * page_counter_dump.sh # dump the cached data every minutes
1 * * * * page_counter_dump.sh # dump the cached data every hour.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-redis-counter-0.1.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file django-redis-counter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-redis-counter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eae79f75ef12a986f8acdb0ab673d32733b5f7ffeb25c212759701b5b4214325
MD5 9a1d1552999bdeff640c23f8ca70bf58
BLAKE2b-256 2a65e6df9f373a5db1cd5141c1565d7c6c0d62c1c7eb52e6c26df4eac19eb530

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page