本文整理汇总了Python中cache.Cache.set方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.set方法的具体用法?Python Cache.set怎么用?Python Cache.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCache
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def testCache(self, n):
m = Cache(n)
# Insert n elements, (0, n]
for i in range(0, n):
m.set(i, i+1)
# Retrieve n elements, (0, n]
for i in range(0, n):
r = random.randrange(0, n)
assert r+1 == m.get(r)
# Insert n elements, (n, n+n]
for i in range(n, n+n):
m.set(i, i+1)
# Retrieve n elements, (n, n+n]
for i in range(0, n):
r = random.randrange(n, n+n)
assert r+1 == m.get(r)
# Retrieve n elements, (0, n]
# Elements should have been deleted from cache
for i in range(0, n):
assert m.get(i) == None
示例2: xtest_evicts_first_key_if_full
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def xtest_evicts_first_key_if_full(self):
cache = Cache(capacity=2)
cache.set('foo', 'bar')
cache.set('baz', 'baz')
cache.set('bar', 'foo')
self.assertEqual(cache.get('foo'), None)
self.assertEqual(cache.get('baz'), 'baz')
self.assertEqual(cache.get('bar'), 'foo')
示例3: cache_url
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def cache_url(self):
'''cache the base_url'''
cache = Cache()
url = self.__get_base_url()
cache.set('url',url,500)
while True:
result = cache.get('url')
if result is None:
result = self.__get_base_url()
cache.set('url',result,500)
return result
示例4: cache_url
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def cache_url(self):
"""cache the base_url"""
cache = Cache()
url = self.__get_base_url()
cache.set("url", url, 500)
while True:
result = cache.get("url")
if result is None:
result = self.__get_base_url()
cache.set("url", result, 500)
return result
示例5: ImageHandler
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class ImageHandler(RequestHandler):
def initialize(self, directory):
self.cache = Cache(directory)
self.twitter_token = twitter.get_bearer()
@coroutine
def get(self, user_id=None):
image = self.cache.get(user_id)
if not image:
print("ImageHandler: {0}: image not present in cache".format(user_id))
url = twitter.get_user_profile_image_url(user_id, self.twitter_token)
if url is not None:
print("ImageHandler: {0}: resolved profile image URL: {1}".format(user_id, url))
if self.cache.set(user_id, url):
print("ImageHandler: {0}: cached image".format(user_id))
# Successfully cached user image.
image = self.cache.get(user_id)
else:
self.fatal("ImageHandler: {0}: failed to cache image".format(user_id))
else:
print("ImageHandler: {0}: failed to fetch profile image URL from Twitter".format(user_id))
image = "./static/default.png"
content_type = self.path_to_content_type(image)
self.set_header("Content-Type", content_type)
with open(image, "r") as f:
self.write(f.read())
def fatal(self, message):
print(message)
raise tornado.web.HTTPError(404)
def path_to_content_type(self, path):
_, extension = os.path.splitext(path)
# Remove leading dot from extension.
return "image/{0}".format(extension[1:])
示例6: a_hard_function
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
#!/usr/bin/env python
#coding=utf-8
from cache import Cache
def a_hard_function():
'''一个需要缓存的函数'''
print "getting result"
from time import sleep
import random
sleep(2)
print "done"
return random.randint(1,100)
if __name__ == "__main__":
cache = Cache()
cache.set('a','aaaa',5) #a的值是'aaaa',生存时间位5秒
cache.set('b',[1,2]) #b的值是[1,2],生存时间无限长
while True:
result = cache.get("hard_func")
if result is None:
result = a_hard_function()
cache.set("hard_func",result,2)
print cache.get('a'),
print cache.get('b'),
print result
示例7: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class Track:
def __init__(self, db=None, artist=None, album=None, min_cache=100,
max_cache=1000, commit_after=100):
"""Create a new track factory.
>>> a = Track(min_cache=1, max_cache=1)
"""
self.__cache_key = Cache(min_cache, max_cache)
self.__cache_id = Cache(min_cache, max_cache)
if db is None:
db = sqlite3.connect(':memory:')
self.__db = db
self.__db.row_factory = dict_factory
self.__db.isolation_level = 'Immediate'
self.__cursor = self.__db.cursor()
if artist is None:
artist = Artist(db=db, min_cache=min_cache, max_cache=max_cache,
commit_after=commit_after)
self.__artist = artist
if album is None:
album = Album(db=db, artist=artist, min_cache=min_cache,
max_cache=max_cache, commit_after=commit_after)
self.__album = album
self.__tbl_name = 'track'
self.__pending_changes = 0
self.__commit_after = commit_after
self.__init_db__()
def __init_db__(self):
sql = '''CREATE TABLE {} (
id INTEGER PRIMARY KEY,
artist_id INTEGER,
album_id INTEGER,
title TEXT,
length INTEGER,
UNIQUE(artist_id, album_id, title, length))'''.format(self.__tbl_name)
self.__cursor.execute('''SELECT * FROM sqlite_master
WHERE type='table' AND name=?''', (self.__tbl_name,))
r = self.__cursor.fetchone()
# FIXME: this is probably not reliable
if not (r and sql == r['sql']):
self.__cursor.execute(sql)
def __key(self, artist, title, length=-1, album=None, album_artist=None):
artist_id = self.__artist.get_id(artist)
if album_artist is None:
album_artist = artist
album_id = self.__album.get_id(album_artist, album)
return u'{}|{}|{}|{}'.format(artist_id, album_id, title, length), \
artist_id, album_id
def get_id(self, artist, title, length=-1, album=None, album_artist=None):
"""return the id of track.
>>> a = Track(min_cache=1, max_cache=1)
>>> a.get_id('The Beatles', 'Please Please Me', 126, 'Please Please Me')
0
>>> a.get_id('The Beatles', 'Please Please Me', 126, 'Please Please Me')
0
"""
title = title.lower()
key, artist_id, album_id = self.__key(artist, title, length, album,
album_artist)
if not self.__cache_key.has_key(key):
track = self.__load(artist_id=artist_id, title=title, album_id=album_id,
length=length)
self.__cache_key.set(key, track['id'])
return track['id']
return self.__cache_key.get(key)
def get(self, track_id):
"""return info from track_id.
>>> a = Track(min_cache=1, max_cache=1)
>>> a.get_id('Taco', "Puttin' on the Ritz", 280, "Hits of the 80's", 'Various Artists')
0
>>> a.get(0)
(u'taco', u"puttin' on the ritz", 280, u"hits of the 80's", u'various artists')
"""
if not self.__cache_id.has_key(track_id):
track = self.__load(track_id=track_id)
if not track:
return None
self.__cache_id.set(track_id, track)
else:
track = self.__cache_id.get(track_id)
artist = self.__artist.get(track['artist_id'])
album_artist, album_title = self.__album.get(track['album_id'])
if album_title == '':
album_title = None
return artist, track['title'], track['length'], album_title, album_artist
def _dump(self):
self.__cursor.execute('''SELECT * FROM {}'''.format(self.__tbl_name))
for row in self.__cursor.fetchall():
print(row)
def __load(self, track_id=None, artist_id=None, title=None, album_id=None,
length=-1):
#.........这里部分代码省略.........
示例8: CacheServer
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class CacheServer(Thread):
"""
Creates an internal cache server that listens on a specified port
"""
def __init__(self, config):
self.__log.info("Instantiating Cache Server")
if config is None:
raise ValueError("Config is not provided")
Thread.__init__(self)
THREAD_NAME = 'cache-server'
self.setName(THREAD_NAME)
self._stop = Event()
self.setDaemon(True)
self.host = config.get('internal_cache', 'cache_server_host')
self.port = config.get('internal_cache', 'cache_server_port')
self.cache_max_size = config.get('internal_cache', 'cache_max_size')
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
server_socket = None
try:
# create a socket object
server_socket = socket.socket()
self.__log.info("Created socket for Cache Server")
server_socket.bind((self.host, int(self.port)))
self.__log.info("Socket is bound to to {}:{}".format(self.host, self.port))
# put the socket into listening mode
server_socket.listen(5)
self.__log.info("Cache Server socket is listening")
except OSError as e:
self.__log.error("Error while creating socket for Cache Server. Error: {}".format(str(e)))
if server_socket is not None:
# create the global cache object
self.__log.info("Creating Global Cache")
self.cache = Cache(self.cache_max_size)
# a forever loop until we interrupt it or an error occurs
while not self.stopped():
try:
# Establish connection with client.
conn, addr = server_socket.accept()
self.__log.info("Connection received from {}".format(addr))
request_data = conn.recv(10240)
self.__log.info("Preparing response")
response = self._handle_request(request_data)
# send the response back to client
conn.send(response)
# Close the connection with the client
conn.close()
except Exception as e:
self.__log.error("Error while processing request. Error: {}".format(str(e)))
conn.close()
finally:
conn.close()
def _handle_request(self, request):
self.__log.info("Request received")
json_data = None
try:
json_data = json.loads(request, 'utf-8')
except Exception as e:
self.__log.error("Error while loading the json request. Error: {}".format(str(e)))
else:
action = json_data["action"]
data = json_data["data"]
key = data["key"]
self.__log.debug("Request data: action: {}, key: {}, data: {}".format(action, key, data))
if action == "set":
value = data["value"] #json.loads(data["value"])
self.cache.set(key, value)
if "ttl" in data.keys():
ex_key = key + "_ttl"
ttl = int(data["ttl"])
self.cache.set(ex_key, ttl)
#self.__log.debug("Testing if the value is set: {}".format(self.cache.get(key)))
return json.dumps(self.cache.get(key))
示例9: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class Album:
def __init__(self, db=None, artist=None, min_cache=100, max_cache=1000,
commit_after=100):
"""Create a new album factory.
>>> a = Album(min_cache=1, max_cache=1)
"""
self.__cache_key = Cache(min_cache, max_cache)
self.__cache_id = Cache(min_cache, max_cache)
if db is None:
db = sqlite3.connect(':memory:')
self.__db = db
self.__db.row_factory = dict_factory
self.__db.isolation_level = 'Immediate'
self.__cursor = self.__db.cursor()
if artist is None:
artist = Artist(db=db, min_cache=min_cache, max_cache=max_cache,
commit_after=commit_after)
self.__artist = artist
self.__tbl_name = 'album'
self.__pending_changes = 0
self.__commit_after = commit_after
self.__init_db__()
def __init_db__(self):
sql = '''CREATE TABLE {} (
id INTEGER PRIMARY KEY,
artist_id INTEGER,
title TEXT,
UNIQUE(artist_id, title))'''.format(self.__tbl_name)
self.__cursor.execute('''SELECT * FROM sqlite_master
WHERE type='table' AND name=?''', (self.__tbl_name,))
r = self.__cursor.fetchone()
# FIXME: this is probably not reliable
if not (r and sql == r['sql']):
self.__cursor.execute(sql)
def __key(self, artist, title):
artist_id = self.__artist.get_id(artist)
return u'{}|{}'.format(artist_id, title), artist_id
def get_id(self, artist, title):
"""return the id of album with title and artist.
>>> a = Album(min_cache=1, max_cache=1)
>>> a.get_id('The Beatles', 'Please Please Me')
0
>>> a.get_id('The BEATles', 'Please PLEASE Me')
0
>>> a.get_id('The Rolling Stones', 'The Rolling Stones')
1
"""
if title is None:
title = u''
else:
title = title.lower()
key, artist_id = self.__key(artist, title)
if not self.__cache_key.has_key(key):
album = self.__load(artist_id=artist_id, title=title)
self.__cache_key.set(key, album['id'])
return album['id']
return self.__cache_key.get(key)
def get(self, album_id):
"""return the name of artist id.
>>> a = Album(min_cache=1, max_cache=1)
>>> a.get_id('The Beatles', 'Please Please Me')
0
>>> a.get_id('The Rolling Stones', 'The Rolling Stones')
1
>>> a.get(0)
(u'the beatles', u'please please me')
>>> a.get(1)
(u'the rolling stones', u'the rolling stones')
"""
if not self.__cache_id.has_key(album_id):
album = self.__load(album_id=album_id)
if not album:
return None
self.__cache_id.set(album_id, album)
else:
album = self.__cache_id.get(album_id)
return self.__artist.get(album['artist_id']), album['title']
def _dump(self):
self.__cursor.execute('''SELECT * FROM {}'''.format(self.__tbl_name))
for row in self.__cursor.fetchall():
print(row)
def __load(self, artist_id=None, title=None, album_id=None):
if artist_id is not None and title is not None:
self.__cursor.execute('''SELECT * FROM {}
WHERE artist_id=? AND title=?'''.format(self.__tbl_name), (
artist_id, title,))
elif album_id is not None:
self.__cursor.execute('''SELECT * FROM {}
WHERE id=?'''.format(self.__tbl_name), (album_id,))
album = self.__cursor.fetchone()
#.........这里部分代码省略.........
示例10: lessened
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class Relation:
"""helper class to provide for simple relations between items. relations
can be strengthend or lessened(?) by giving them a positive or negative
'nudge'.
>>> r = Relation('test')
>>> r.get(100, 200)
0.5
>>> '{:2.2f}'.format(r.positive(100, 200))
'0.71'
>>> '{:2.2f}'.format(r.negative(100, 200))
'0.43'
>>> '{:2.2f}'.format(r.get(100, 200))
'0.43'
>>> r.get(100, 200, 'pos')
1.0
>>> r.get(100, 200, 'neg')
1.0
"""
def __init__(self, name, db=None, mix=0.5, min_cache=100, max_cache=1000,
commit_after=100):
"""create a new relation class.
>>> db = sqlite3.connect(':memory:')
>>> r = Relation('test', db=db, mix=0.75, min_cache=100, max_cache=200)
>>> r.get(100, 200)
0.5
>>> '{:2.2f}'.format(r.positive(100, 200))
'0.73'
>>> del r
>>> r = Relation('test', db=db, mix=0.75, min_cache=100, max_cache=200)
>>> '{:2.2f}'.format(r.get(100, 200))
'0.73'
"""
self.__name = name
# for caching
self.__cache = Cache(min_cache, max_cache)
# factors for calculating the relation
self.__rolling = mix
self.__stable = 1.0 - mix
# commit changes to the database after save_after changes
self.__commit_after = commit_after
self.__pending_changes = 0
self.__tbl_name = 'relation_{}'.format(self.__name)
if db is None:
db = sqlite3.connect(':memory:')
self.__db = db
self.__db.row_factory = dict_factory
self.__db.isolation_level = 'Immediate'
self.__cursor = self.__db.cursor()
self.__init_db__()
def __init_db__(self):
sql = '''CREATE TABLE {} (
keyl INTEGER,
keyh INTEGER,
pos REAL,
neg REAL,
rel REAL,
UNIQUE(keyl, keyh))'''.format(self.__tbl_name)
self.__cursor.execute('''SELECT * FROM sqlite_master
WHERE type='table' AND name=?''', (self.__tbl_name,))
r = self.__cursor.fetchone()
# FIXME: this is probably not reliable
if not (r and sql == r['sql']):
self.__cursor.execute(sql)
def min_cache(self, min_cache=None):
"""set minimum cache size.
>>> r = Relation('test', min_cache=100)
>>> r.min_cache()
100
>>> r.min_cache(200)
200
>>> r.min_cache()
200
"""
return self.__cache.min_cache(min_cache)
def max_cache(self, max_cache=None):
"""set maximum cache size.
>>> r = Relation('test', max_cache=1000)
>>> r.max_cache()
1000
>>> r.max_cache(200)
200
>>> r.max_cache()
200
"""
return self.__cache.max_cache(max_cache)
def cache_size(self):
"""return the current size of the cache.
>>> r = Relation('test')
>>> r.cache_size()
0
#.........这里部分代码省略.........
示例11: test_evicts_oldest_key_if_full_and_no_key_is_ever_accessed
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def test_evicts_oldest_key_if_full_and_no_key_is_ever_accessed(self):
cache = Cache(capacity=1)
cache.set('foo', 'bar')
cache.set('baz', 'baz')
cache.set('bar', 'foo')
self.assertEqual(cache.get('foo'), None)
示例12: test_returns_existing_value_if_key_exist
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def test_returns_existing_value_if_key_exist(self):
cache = Cache()
value = cache.set('foo', 'rab')
value = cache.get_or_set('foo', lambda: 'bar')
self.assertEqual(value, 'rab')
示例13: test_returns_none_after_flush
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def test_returns_none_after_flush(self):
cache = Cache()
cache.set('foo', 'bar')
cache.flush()
value = cache.get('foo')
self.assertEqual(value, None)
示例14: test_returns_value_if_key_exists
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
def test_returns_value_if_key_exists(self):
cache = Cache()
cache.set('foo', 'bar')
value = cache.get('foo')
self.assertEqual(value, 'bar')
示例15: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import set [as 别名]
class App:
def __init__(self, serverId):
# Open DB connection
self.db = memcache.Client([DB_ADDRESS[serverId]], debug=0)
# Initialize cache
self.cache = Cache(MAX_CACHE_ENTRIES)
# Initialize the nextCodeCount, used to generate short URLs
self.nextCodeCount = self._get_code_count()
# Prefix of the short URLs generated by this app instance
self.prefix = PREFIXES[serverId]
def get_short_url(self, longUrl):
# Generate a code for the URL
code = self.prefix + self._get_next_code()
# Check if URL starts with valid schema id (http or https)
if not (longUrl[:7] == "http://" or longUrl[:8] == "https://"):
# If not add schema id to url (needed to redirect when requested)
longUrl = 'http://' + longUrl
# Store in the DB and the Cache the mapping between the URL and the code
self.db.set(code, longUrl)
self.cache.set(code, longUrl)
# Return short url
return code
def get_long_url(self, shortUrl):
# Try to get original URL from Cache
longUrl = self.cache.get(shortUrl)
if not longUrl:
# If not in cache, get it from DB
longUrl = self.db.get(shortUrl)
if longUrl:
# Add the retreived URL to the cache
self.cache.set(shortUrl, longUrl)
return longUrl
def _get_code_count(self):
# Fetch from DB the stored value for nextCodeCount
lastCodeCount = self.db.get("_nextCodeCount")
if lastCodeCount:
return lastCodeCount
else:
# If it is not in DB, start it to 0
return 0
def _get_next_code(self):
# Get code from nextCodeCount
code = self._alphabet_encode(self.nextCodeCount)
# Update nextCodeCount
self.nextCodeCount += 1
# Update _nextCodeCount in DB (used when the app is restarted)
self.db.set("_nextCodeCount", self.nextCodeCount)
return code
def _alphabet_encode(self, num):
if num == 0:
return ALPHABET[0]
code = ''
while num != 0:
num, i = divmod(num, len(ALPHABET))
code = ALPHABET[i] + code
# Return nextCodeCount represented using ALPHABET
return code