Completed
Push — master ( e6b198...e30f90 )
by Messense
11:09 queued 10:06
created

MemcachedStorage.key_name()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 2
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
cc 1
dl 2
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from wechatpy.session import SessionStorage
4 10
from wechatpy.utils import to_text
5 10
from wechatpy.utils import json
6
7
8 10 View Code Duplication
class MemcachedStorage(SessionStorage):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
10 10
    def __init__(self, mc, prefix='wechatpy'):
11 10
        for method_name in ('get', 'set', 'delete'):
12 10
            assert hasattr(mc, method_name)
13 10
        self.mc = mc
14 10
        self.prefix = prefix
15
16 10
    def key_name(self, key):
17 10
        return '{0}:{1}'.format(self.prefix, key)
18
19 10
    def get(self, key, default=None):
20 10
        key = self.key_name(key)
21 10
        value = self.mc.get(key)
22 10
        if value is None:
23
            return default
24 10
        return json.loads(to_text(value))
25
26 10
    def set(self, key, value, ttl=None):
27 10
        if value is None:
28 10
            return
29 10
        key = self.key_name(key)
30 10
        value = json.dumps(value)
31 10
        self.mc.set(key, value)
32
33 10
    def delete(self, key):
34
        key = self.key_name(key)
35
        self.mc.delete(key)
36