Completed
Pull Request — master (#138)
by
unknown
20:13
created

MemcachedStorage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 28
Duplicated Lines 100 %

Test Coverage

Coverage 86.96%
Metric Value
dl 28
loc 28
ccs 20
cts 23
cp 0.8696
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 6 6 2
A delete() 3 3 1
A get() 6 6 2
A key_name() 2 2 1
A __init__() 5 5 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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