├── pyLruCache ├── __init__.py ├── __init__.pyc ├── pyLruCache.pyc └── pyLruCache.py ├── MANIFEST.in ├── test_list.py ├── setup.py ├── LICENSE └── README.md /pyLruCache/__init__.py: -------------------------------------------------------------------------------- 1 | from .pyLruCache import * 2 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | recursive-include README.md 3 | -------------------------------------------------------------------------------- /pyLruCache/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfyiamcool/pyLruCache/HEAD/pyLruCache/__init__.pyc -------------------------------------------------------------------------------- /pyLruCache/pyLruCache.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rfyiamcool/pyLruCache/HEAD/pyLruCache/pyLruCache.pyc -------------------------------------------------------------------------------- /test_list.py: -------------------------------------------------------------------------------- 1 | from pyLruCache import pyLruListCache 2 | 3 | a = pyLruListCache(5) 4 | 5 | 6 | for i in range(100): 7 | a.appendd(i) 8 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup(name='pyLruCache', 4 | version='3.0.1', 5 | description='Use Lru dict for cache.', 6 | url='http://github.com/rfyiamcool', 7 | author='ruifengyun', 8 | author_email='rfyiamcool@163.com', 9 | long_description=open('README.md').read(), 10 | license='MIT', 11 | packages=['pyLruCache'], 12 | keywords = "lru cache author ruifengyun", 13 | classifiers = [ 14 | 'Topic :: Utilities', 15 | 'License :: OSI Approved :: MIT License' 16 | ] 17 | 18 | ) 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 fengyun rui 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 项目 2 | 3 | pyLruCache.py 4 | 5 | 6 | ### 介绍 7 | 8 | Lru适合做定量的数据新旧替换的算法,特别适合做缓存的应用,后期会考虑接入redis做存储。 9 | 10 | 11 | ```python 12 | 13 | from pyLruCache import * 14 | In [1]: from pyLruCache import * 15 | 16 | In [2]: a = pyLruListCache(3) 17 | 18 | In [3]: for i in a.iteritems(): 19 | ...: print i 20 | ...: 21 | 22 | In [4]: 23 | 24 | In [4]: a[1] = 1 25 | 26 | In [5]: a[2] = 2 27 | 28 | In [6]: a[3] = 3 29 | 30 | In [7]: for i in a.iteritems(): 31 | ...: print i 32 | ...: 33 | (1, 1) 34 | (2, 2) 35 | (3, 3) 36 | 37 | In [8]: a[4] = 4 38 | 39 | In [9]: for i in a.iteritems(): 40 | print i 41 | ...: 42 | (2, 2) 43 | (3, 3) 44 | (4, 4) 45 | 46 | In [10]: print a[2] 47 | 2 48 | 49 | In [11]: a[5] = 5 50 | 51 | In [12]: for i in a.iteritems(): 52 | print i 53 | ....: 54 | (4, 4) 55 | (2, 2) 56 | (5, 5) 57 | 58 | In [13]: a[6] = [] 59 | 60 | In [14]: a[6].append(1) 61 | 62 | In [15]: a[6].append(2) 63 | 64 | In [16]: a[6].append(3) 65 | 66 | In [17]: for i in a.iteritems(): 67 | print i 68 | ....: 69 | (2, 2) 70 | (5, 5) 71 | (6, [1, 2, 3]) 72 | 73 | In [18]: a[7]=7 74 | 75 | In [19]: for i in a.iteritems(): 76 | print i 77 | ....: 78 | (5, 5) 79 | (6, [1, 2, 3]) 80 | (7, 7) 81 | ``` 82 | 83 | ### for List 84 | 85 | ```python 86 | rom pyLruCache import pyLruListCache 87 | 88 | a = pyLruListCache(5) 89 | 90 | for i in range(100): 91 | a.appendd(i) 92 | ``` 93 | 94 | stdout: 95 | 96 | ``` 97 | {96: 96, 92: 92, 93: 93, 94: 94, 95: 95} 98 | 92 99 | 100 | {96: 96, 97: 97, 93: 93, 94: 94, 95: 95} 101 | 93 102 | 103 | {96: 96, 97: 97, 98: 98, 94: 94, 95: 95} 104 | ``` 105 | 106 | ## Install 107 | 108 | pyLruCache can be installed using Pypi 109 | 110 | `pip install pyLruCache` 111 | 112 | ## Detail 113 | 114 | 详情: xiaorui.cc 115 | 116 | ## To Do List 117 | 118 | 1. support redis broker 119 | 120 | -------------------------------------------------------------------------------- /pyLruCache/pyLruCache.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | class Node(object): 3 | __slots__ = ['prev', 'next', 'me'] 4 | def __init__(self, prev, me): 5 | self.prev = prev 6 | self.me = me 7 | self.next = None 8 | 9 | class pyLruListCache: 10 | def __init__(self, count): 11 | self.count = count 12 | self.l = [] 13 | self.lru = {} 14 | self.tm = 0 15 | 16 | def extend(self,t_list): 17 | for i in t_list: 18 | self.lru[item] = self.tm 19 | self.tm += 1 20 | self.append(i) 21 | 22 | def appendd(self, item): 23 | if len(self.l)>=self.count: 24 | old_key = min(self.lru.keys(), key=lambda k:self.lru[k]) 25 | self.l.remove(old_key) 26 | self.lru.pop(old_key) 27 | self.l.append(item) 28 | self.lru[item] = self.tm 29 | self.tm += 1 30 | 31 | def pop(self): 32 | old_key = min(self.lru.keys(), key=lambda k:self.lru[k]) 33 | self.l.remove(old_key) 34 | return old_key 35 | 36 | def __getitem__(self,item): 37 | data = self.l[item] 38 | self.lru[item] = self.tm 39 | self.tm += 1 40 | return data 41 | 42 | class pyLruDictCache: 43 | """ for dict """ 44 | def __init__(self, count, pairs=[]): 45 | self.count = max(count, 1) 46 | self.d = {} 47 | self.first = None 48 | self.last = None 49 | for key, value in pairs: 50 | self[key] = value 51 | def __contains__(self, obj): 52 | return obj in self.d 53 | def __getitem__(self, obj): 54 | a = self.d[obj].me 55 | self[a[0]] = a[1] 56 | return a[1] 57 | def __setitem__(self, obj, val): 58 | if obj in self.d: 59 | del self[obj] 60 | nobj = Node(self.last, (obj, val)) 61 | if self.first is None: 62 | self.first = nobj 63 | if self.last: 64 | self.last.next = nobj 65 | self.last = nobj 66 | self.d[obj] = nobj 67 | if len(self.d) > self.count: 68 | if self.first == self.last: 69 | self.first = None 70 | self.last = None 71 | return 72 | a = self.first 73 | a.next.prev = None 74 | self.first = a.next 75 | a.next = None 76 | del self.d[a.me[0]] 77 | del a 78 | def __delitem__(self, obj): 79 | nobj = self.d[obj] 80 | if nobj.prev: 81 | nobj.prev.next = nobj.next 82 | else: 83 | self.first = nobj.next 84 | if nobj.next: 85 | nobj.next.prev = nobj.prev 86 | else: 87 | self.last = nobj.prev 88 | del self.d[obj] 89 | def __iter__(self): 90 | cur = self.first 91 | while cur != None: 92 | cur2 = cur.next 93 | yield cur.me[1] 94 | cur = cur2 95 | def iteritems(self): 96 | cur = self.first 97 | while cur != None: 98 | cur2 = cur.next 99 | yield cur.me 100 | cur = cur2 101 | def iterkeys(self): 102 | return iter(self.d) 103 | def itervalues(self): 104 | for i,j in self.iteritems(): 105 | yield j 106 | def keys(self): 107 | return self.d.keys() 108 | --------------------------------------------------------------------------------