Skip to main content

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 drc-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.

Metadata

Release files for django-redis-counter 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-redis-counter 0.1.1
File Size Uploaded
django-redis-counter-0.1.1.tar.gz 4.0 kB Details

Release files / django-redis-counter-0.1.1.tar.gz

Download URL django-redis-counter-0.1.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
af64625dd1f09a1f986e052d4982f6156687d53010c2983a26e28660bd72ef33
BLAKE2b-256 checksum
How to use checksums
98df7ededad31f1f34e3a7dff62ef47e3d742cda4ef820e3b1ac6fb054c133b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page