Skip to content

Instantly share code, notes, and snippets.

@binux
Created January 6, 2012 11:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save binux/1570223 to your computer and use it in GitHub Desktop.
Save binux/1570223 to your computer and use it in GitHub Desktop.
一个通用的python function cache
# -*- coding: utf-8 -*-
#
# Copyright(c) 2010 poweredsites.org
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
from time import time
_mem_caches = {}
def mem_cache(expire=7200, key=""):
"""Mem cache to python dict by key"""
def wrapper(func):
def new_func(self, *args, **kwargs):
now = time()
if key:
c = key
else:
c = self.__class__.__name__ + func.__name__ + repr(func)
k = key_gen(c, *args, **kwargs)
value = _mem_caches.get(k, None)
if _valid_cache(value, now):
return value["value"]
else:
val = func(*args, **kwargs)
_mem_caches[k] = {"value":val, "expire":now+expire}
return val
return new_func
return wrapper
def key_gen(key, *args, **kwargs):
code = hashlib.md5()
code.update(str(key))
code.update("".join(map(str, args)))
code.update("".join(sorted(map(str, kwargs.iteritems()))))
return code.hexdigest()
def _valid_cache(value, now):
if value:
if value["expire"] > now:
return True
else:
return False
else:
return False
# -*- coding: utf-8 -*-
#
# Copyright(c) 2010 poweredsites.org
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import hashlib
from time import time
def mem_cache(expire=7200, key=""):
"""Mem cache to python dict by function"""
_mem_caches = {}
def wrapper(func):
def new_func(*args, **kwargs):
now = time()
if key:
c = key
else:
c = func.__name__ + repr(func)
k = key_gen(c, *args, **kwargs)
value = _mem_caches.get(k, None)
if _valid_cache(value, now):
return value["value"]
else:
val = func(*args, **kwargs)
_mem_caches[k] = {"value":val, "expire":now+expire}
return val
return new_func
return wrapper
def key_gen(*args, **kwargs):
code = hashlib.md5()
code.update("".join(map(str, args)))
code.update("".join(sorted(map(str, kwargs.iteritems()))))
return code.hexdigest()
def _valid_cache(value, now):
if value:
if value["expire"] > now:
return True
else:
return False
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment