Python 缓存机制与 functools.lru_cache

缓存概念

缓存是一种将定量数据加以保存以备迎合后续请求的处理方式,目的是为了加快数据的检索速度
下面看一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
import datetime
import random
class MyCache:
""""""
def __init__(self):
"""Constructor"""
self.cache = {}
self.max_cache_size = 10
def __contains__(self, key):
"""
根据该键是否存在于缓存当中返回True或者False
"""
return key in self.cache
def update(self, key, value):
"""
更新该缓存字典,您可选择性删除最早条目
"""
if key not in self.cache and len(self.cache) >= self.max_cache_size:
self.remove_oldest()
self.cache[key] = {'date_accessed': datetime.datetime.now(),
'value': value}
def remove_oldest(self):
"""
删除具备最早访问日期的输入数据
"""
oldest_entry = None
for key in self.cache:
if oldest_entry == None:
oldest_entry = key
elif self.cache[key]['date_accessed'] < self.cache[oldest_entry]['date_accessed']:
oldest_entry = key
self.cache.pop(oldest_entry)
@property
def size(self):
"""
返回缓存容量大小
"""
return len(self.cache)
if __name__ == '__main__':
#测试缓存
keys = ['test', 'red', 'fox', 'fence', 'junk', \
'other', 'alpha', 'bravo', 'cal', 'devo', 'ele']
s = 'abcdefghijklmnop'
cache = MyCache()
for i, key in enumerate(keys):
if key in cache:
continue
else:
value = ''.join([random.choice(s) for i in range(20)])
cache.update(key, value)
print("#%s iterations, #%s cached entries" % (i+1, cache.size))

阅读全文

谷歌浏览器常用快捷键

Chrome窗口和标签页快捷键:

  1. Ctrl+N 打开新窗口

阅读全文

MongoDB配置

安装mongodb

安装就免了吧,安装好mongodb,然后配置环境变量

创建MongoDB配置文件

注意: 配置文件中的所有路径必须要存在, mongodb不会自动给你创建文件夹,所以像data,log,pid等文件夹按照配置文件指定路径手动创建好,否则会报错.
先创建一个配置文件,名称为mongodb.cfg,配置内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#数据文件存放位置
dbpath=F:/mongodb/data/db
#日志文件存放位置
logpath=F:/mongodb/data/log/mongodb.log
#PID的路径
pidfilepath=F:/mongodb/pid/mongodb.pid
#端口号
port=27017
#错误日志采用追加模式,配置这个选项后mongodb的日志会追加到现有的日志文件
logappend=true
#启用日志文件,默认启用
journal=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true
#打开28017网页端口(若不开启注释掉即可)
rest=true

阅读全文

Python定时器

python中定时器

python中的定时器在threading模块中,而且只执行一次, 那么如何定时循环调用呢?
Timer: 隔一定时间调用一个函数,如果想实现每隔一段时间就调用一个函数的话,就要在Timer调用的函数中,再次设置Timer。
Timer其实是Thread的一个派生类

阅读全文

Linux一些常用命令

scp命令

scp是secure copy的简写,用于在Linux下进行远程拷贝文件或文件夹.scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然 rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。

阅读全文