Browse Source

Create basic misago.cache application

rafalp 6 years ago
parent
commit
cfdb9d5339

+ 1 - 0
devproject/settings.py

@@ -188,6 +188,7 @@ INSTALLED_APPS = [
     # Misago apps
     'misago.admin',
     'misago.acl',
+    'misago.cache',
     'misago.core',
     'misago.conf',
     'misago.markup',

+ 2 - 10
misago/acl/migrations/0002_acl_version_tracker.py

@@ -1,20 +1,12 @@
 from django.db import migrations
 
-from misago.acl.constants import ACL_CACHEBUSTER
-from misago.core.migrationutils import cachebuster_register_cache
-
-
-def register_acl_version_tracker(apps, schema_editor):
-    cachebuster_register_cache(apps, ACL_CACHEBUSTER)
-
 
 class Migration(migrations.Migration):
+    """Superseded by 0004"""
 
     dependencies = [
         ('misago_acl', '0001_initial'),
         ('misago_core', '0001_initial'),
     ]
 
-    operations = [
-        migrations.RunPython(register_acl_version_tracker),
-    ]
+    operations = []

+ 16 - 0
misago/acl/migrations/0004_cache_version.py

@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+from django.db import migrations
+
+from misago.cache.operations import StartCacheVersioning
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('misago_acl', '0003_default_roles'),
+        ('misago_cache', '0001_initial'),
+    ]
+
+    operations = [
+        StartCacheVersioning("acl")
+    ]

+ 1 - 0
misago/cache/__init__.py

@@ -0,0 +1 @@
+default_app_config = 'misago.cache.apps.MisagoCacheConfig'

+ 7 - 0
misago/cache/apps.py

@@ -0,0 +1,7 @@
+from django.apps import AppConfig
+
+
+class MisagoCacheConfig(AppConfig):
+    name = 'misago.cache'
+    label = 'misago_cache'
+    verbose_name = "Misago Cache"

+ 10 - 0
misago/cache/middleware.py

@@ -0,0 +1,10 @@
+from django.utils.deprecation import MiddlewareMixin
+
+from .utils import get_cache_versions
+
+
+def cache_versions_middleware(get_response):
+    """Sets request.cache_versions attribute with dict cache versions."""
+    def middleware(request):
+        request.cache_versions = get_cache_versions()
+        return get_response(request)

+ 24 - 0
misago/cache/migrations/0001_initial.py

@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.16 on 2018-11-25 15:15
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import misago.cache.utils
+
+
+class Migration(migrations.Migration):
+
+    initial = True
+
+    dependencies = [
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='CacheVersion',
+            fields=[
+                ('cache', models.CharField(max_length=128, primary_key=True, serialize=False)),
+                ('version', models.CharField(default=misago.cache.utils.get_random_version, max_length=8)),
+            ],
+        ),
+    ]

+ 0 - 0
misago/cache/migrations/__init__.py


+ 8 - 0
misago/cache/models.py

@@ -0,0 +1,8 @@
+from django.db import models
+
+from .utils import get_random_version
+
+
+class CacheVersion(models.Model):
+    cache = models.CharField(max_length=128, primary_key=True)
+    version = models.CharField(max_length=8, default=get_random_version)

+ 29 - 0
misago/cache/operations.py

@@ -0,0 +1,29 @@
+from django.db.migrations import RunPython
+
+
+class StartCacheVersioning(RunPython):
+    def __init__(self, cache):
+        code = start_cache_versioning(cache)
+        reverse_code = stop_cache_versioning(cache)
+        super().__init__(code, reverse_code)
+
+
+class StopCacheVersioning(RunPython):
+    def __init__(self, cache):
+        code = stop_cache_versioning(cache)
+        reverse_code = start_cache_versioning(cache)
+        super().__init__(code, reverse_code)
+
+
+def start_cache_versioning(cache):
+    def migration_operation(apps, _):
+        CacheVersion = apps.get_model('misago_cache', 'CacheVersion')
+        CacheVersion.objects.create(cache=cache)
+    return migration_operation
+
+
+def stop_cache_versioning(cache):
+    def migration_operation(apps, _):
+        CacheVersion = apps.get_model('misago_cache', 'CacheVersion')
+        CacheVersion.objects.filter(cache=cache).delete()
+    return migration_operation

+ 5 - 0
misago/cache/utils.py

@@ -0,0 +1,5 @@
+from django.utils.crypto import get_random_string
+
+
+def get_random_version():
+    return get_random_string(8)

+ 2 - 10
misago/users/migrations/0003_bans_version_tracker.py

@@ -1,20 +1,12 @@
 from django.db import migrations
 
-from misago.core.migrationutils import cachebuster_register_cache
-from misago.users.constants import BANS_CACHEBUSTER
-
-
-def register_bans_version_tracker(apps, schema_editor):
-    cachebuster_register_cache(apps, BANS_CACHEBUSTER)
-
 
 class Migration(migrations.Migration):
+    """Migration superseded by 0016"""
 
     dependencies = [
         ('misago_users', '0002_users_settings'),
         ('misago_core', '0001_initial'),
     ]
 
-    operations = [
-        migrations.RunPython(register_bans_version_tracker),
-    ]
+    operations = []

+ 16 - 0
misago/users/migrations/0016_cache_version.py

@@ -0,0 +1,16 @@
+# Generated by Django 1.11.16 on 2018-11-25 15:31
+from django.db import migrations
+
+from misago.cache.operations import StartCacheVersioning
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('misago_users', '0015_user_agreements'),
+        ('misago_cache', '0001_initial'),
+    ]
+
+    operations = [
+        StartCacheVersioning("bans")
+    ]