~mpontillo/maas/dns-template-changes-1.7

« back to all changes in this revision

Viewing changes to src/provisioningserver/cache.py

  • Committer: MaaS Lander
  • Author(s): Gavin Panella
  • Date: 2014-09-23 21:21:43 UTC
  • mfrom: (3064.2.10 rpc-dns)
  • Revision ID: maas_lander-20140923212143-tjgdnu2f3tjd2pmd
[r=blake-rouse][bug=][author=allenap] Purge all remaining Celery code from the cluster controller code.

Code in provisioningserver relating to DNS still uses Celery, but it is not used on the cluster controllers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2012-2014 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""API credentials for node-group workers."""
5
 
 
6
 
from __future__ import (
7
 
    absolute_import,
8
 
    print_function,
9
 
    unicode_literals,
10
 
    )
11
 
 
12
 
str = None
13
 
 
14
 
__metaclass__ = type
15
 
__all__ = [
16
 
    'cache',
17
 
    'initialize',
18
 
    ]
19
 
 
20
 
 
21
 
from multiprocessing import Manager
22
 
 
23
 
 
24
 
class Cache:
25
 
    """A process-safe dict-like cache."""
26
 
 
27
 
    def __init__(self, cache_backend):
28
 
        self.cache_backend = cache_backend
29
 
 
30
 
    def set(self, key, value):
31
 
        self.cache_backend[key] = value
32
 
 
33
 
    def get(self, key):
34
 
        return self.cache_backend.get(key, None)
35
 
 
36
 
    def clear(self):
37
 
        self.cache_backend.clear()
38
 
 
39
 
 
40
 
_manager = None
41
 
 
42
 
cache = None
43
 
 
44
 
initialized = False
45
 
 
46
 
 
47
 
def initialize():
48
 
    """Initialize cache of shared data between processes.
49
 
 
50
 
    This needs to be done exactly once, by the parent process, before it
51
 
    start forking off workers.
52
 
    """
53
 
    global _manager
54
 
    global cache
55
 
    global initialized
56
 
    if not initialized:
57
 
        _manager = Manager()
58
 
        cache = Cache(_manager.dict())
59
 
        initialized = True