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

MemcachedStorage.set()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
cc 2
dl 6
loc 6
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
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