Python Advanced Programming: Caching

Source: Internet
Author: User
Tags md5 hash

#-*-Coding:utf-8-*-
__author__ = ' Administrator '
#缓存
"""
For functions and method results that are expensive to run, you can cache them as long as:
1: The function is deterministic, enter the same value, the resulting results are the same every time;
2: Function return value is continuously useful and effective for a certain period of time (indeterminate)

--------
A deterministic function is the same parameter that will always return the same result, while the nondeterministic function returns results that may change
"""
#较好的缓存候选者通常是:
"""
Callable object results from the query database
Callable object results from rendering static values (like file content, Web requests, or PDF display)
Callable object results from deterministic, executed complex computations
Record numeric global mappings that have an expiration time, such as a Web conversation object
Some require frequent and fast access to the data.
"""
#1确定性缓存
#计算平方数函数的例子
Import Random,timeit
Import Profile,print_stats
cache={}
def s (n):
Return N*n
@profile (' not cache ')
def f ():
For I in Xrange (100):
S (Random.randint (1,10))
def cache_f (n):
If n not in cache:
Cache[n]=s (N)
return Cache[n]
@profile (' cache ')
Def c_f ():
N=[random.randint (1,10) for I in range (10)]
Ns=[cache_f (i) for i in N]

Print f (), C_f ()
Print Print_stats ()
"""
Of course, as long as the interaction with the cache takes less time than the function, the cache is valid, and if the value is recalculated faster, the governess must do so, and if used incorrectly, the cache can be dangerous
"""
#例子2
"""
In the previous example, using the parameters of a function as the key value of the cache, only when the argument can be hash is valid, when the parameters are complex, and not necessarily hash. They must be processed manually and converted to a unique key value for caching
"""
def me (a,b,c,d):
Key= ' Cache Me:::%s%s '% (a,b,c)
If key not in key:
print ' Caching '
Cache[key]=complex_calculation (A,B,C,D)
Print D
return Cache[key]
"""
Of course, it is possible to iterate through each parameter to automatically create a key value, but there are many special cases where you will need to manually calculate the key value, so that the behavior is called a comment, you can use the adorner to handle
"""
#如下:
cache={}
Def get_key (function, *a,**b):
key= '%s.%s '% (function.__module__,function.__name__)
HASH_ARGS=[STR (A1) for A1 in a]
#当然, can only be used when V is hashed
kv=['%s:%s '% (K,hash (v)) for k,v in B.items ()]
Return '%s:%s:%s '% (KEY,HASH_ARGS,KV)
def memioze (Get_key=get_key,caheck=cache):
def _m (func):
def _me (*a,**b):
Key=get_key (FUNC,*A,**B)
Try
return Caheck[key]
Except Keyerror as S:
Caheck[key]=func (*A,**B)
return value
Return _me
Return _m
@memioze ()
def faction1 (n):
Return N*n

Faction1 (4)
Print Cache
"""
This adorner uses a callable object to calculate the key value, the default Get_key will be the parameter introspection, if the keyword cannot hash, then an exception occurs
However, this function is only used for special cases, and the mapping of stored values can be configured

The method is to calculate the MD5 hash or (SHA) of the parameters, but know that such a hash has a real cost, the function itself is faster than the key value calculation, the cache is meaningful
"""
#例子3
Import MD5
def key1 (fuction1,n):
return Md5.md5 (STR (n)). Hexdigest ()
@memioze (Key1)
def N (N1):
Return n1**2

------------------

#-*-Coding:utf-8-*-
__author__ = ' Administrator '
#非确定性缓存
"""
For nondeterministic functions, it is possible to produce different inputs even when specifying the same input
The cache duration is set according to the average update time of the data
"""
#例子
c={}
def m (get_kye=get_key,s=c,arg=0):
Def _m (f):
def __ (*a,**b):
Key=get_kye (F,*A,**B)
Try
V1,v2=s[key]
Exp= (Arg!=0 and (v1) <time.time ())
Except Keyerror as S:
Exp=true
If not exp:
Return v2
S[key]=time.time (), F (*a,**b)
return s[key][1]
Return __m
Return _m
#假设有一个显示当前时间的函数, if the number of seconds is not displayed, you can cache it for 30 seconds to get a reasonable precision cache value
From datetime import datetime
@m (age=30)
Def w_t ():
Return DateTime.Now (). Strftime ('%h:%m ')
w_t ()
C
"""
Of course, cache deprecation must be implemented asynchronously by another function that removes the expiration key to accelerate the M function, which often appears in the Web
-----------
Active buffering
There are many caching policies that can be used to speed up applications
MEMCACHED, if the cache requirements are high you can use it (http://www.danga.com/memcached)
, it is UNIX, it can be driven in other platforms and other languages
More can see: (Http://pypi.python.org/pypi/Beakey) "" "

Python Advanced Programming: Caching

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.