|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
from django.conf import settings |
|
3
|
|
|
from django.core.cache import caches |
|
4
|
|
|
from django_th.my_services import MyService |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class PublishingLimit(object): |
|
8
|
|
|
|
|
9
|
|
|
""" |
|
10
|
|
|
this class permits to reduce the quantity of data to be pulibshed |
|
11
|
|
|
get the limit from settings.DJANGO_TH['publishing_limit'] |
|
12
|
|
|
if the limit does not exist, it returns everything |
|
13
|
|
|
""" |
|
14
|
|
|
@staticmethod |
|
15
|
|
|
def get_data(service, cache_data, trigger_id): |
|
16
|
|
|
""" |
|
17
|
|
|
get the data from the cache |
|
18
|
|
|
:param service: the service name |
|
19
|
|
|
:param cache_data: the data from the cache |
|
20
|
|
|
:type trigger_id: integer |
|
21
|
|
|
:return: Return the data from the cache |
|
22
|
|
|
:rtype: object |
|
23
|
|
|
""" |
|
24
|
|
|
|
|
25
|
|
|
# rebuild the string |
|
26
|
|
|
# th_<service>.my_<service>.Service<Service> |
|
27
|
|
|
if service.startswith('th_'): |
|
28
|
|
|
service_long = MyService.full_name(service) |
|
29
|
|
|
# ... and check it |
|
30
|
|
|
if service_long in settings.TH_SERVICES: |
|
31
|
|
|
|
|
32
|
|
|
cache = caches['django_th'] |
|
33
|
|
|
|
|
34
|
|
|
limit = settings.DJANGO_TH.get('publishing_limit', 0) |
|
35
|
|
|
|
|
36
|
|
|
# publishing of all the data |
|
37
|
|
|
if limit == 0: |
|
38
|
|
|
return cache_data |
|
39
|
|
|
# or just a set of them |
|
40
|
|
|
if cache_data is not None and len(cache_data) > limit: |
|
41
|
|
|
for data in cache_data[limit:]: |
|
42
|
|
|
service_str = ''.join((service, '_', |
|
43
|
|
|
str(trigger_id))) |
|
44
|
|
|
# put that data in a version 2 of the cache |
|
45
|
|
|
cache.set(service_str, data, version=2) |
|
46
|
|
|
# delete data from cache version=1 |
|
47
|
|
|
# https://niwinz.github.io/django-redis/latest/#_scan_delete_keys_in_bulk |
|
48
|
|
|
cache.delete_pattern(service_str) |
|
49
|
|
|
# put in cache unpublished data |
|
50
|
|
|
cache_data = cache_data[:limit] |
|
51
|
|
|
|
|
52
|
|
|
return cache_data |
|
53
|
|
|
|