Passed
Push — master ( 98a4f0...2943be )
by dai
02:10
created

school_api.session.memorystorage.MemoryStorage.expires_time()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 3
dl 3
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
from __future__ import absolute_import, unicode_literals
3
import time
4
from school_api.session import SessionStorage
5
from school_api.utils import to_text
6
7
8 View Code Duplication
class MemoryStorage(SessionStorage):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
9
    ''' 非持久化缓存 不推荐使用'''
10
11
    def __init__(self, prefix=''):
12
        self._data = {}
13
        self.prefix = to_text(prefix)
14
15
    def key_name(self, key):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
16
        return '{0}:{1}'.format(self.prefix, key)
17
18
    def get(self, key, default=None):
19
        key = self.key_name(key)
20
        value = None
21
        data = self._data.get(key, default)
22
        if data:
23
            if data['expires_at'] is True or data['expires_at'] > time.time():
24
                # 为True时:永不过期, 大于当前时间为:不到过期时间
25
                value = data['value']
26
            else:
27
                self.delete(key)
28
        return value
29
30
    def set(self, key, value, ttl=None):
31
32
        if value is None:
33
            return
34
35
        key = self.key_name(key)
36
        expires_at = not ttl or time.time() + ttl
37
        data = {'value': value, 'expires_at': expires_at}
38
        self._data[key] = data
39
40
    def delete(self, key):
41
        key = self.key_name(key)
42
        self._data.pop(key, None)
43
44
    def expires_time(self, key):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
45
        data = self._data.get(self.key_name(key))
46
        return data['expires_at'] - time.time()
47