本文整理汇总了Python中cache.Cache.put方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.put方法的具体用法?Python Cache.put怎么用?Python Cache.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.put方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
def predict(self, localities):
"""Predict locality type for each locality in a list."""
for loc in localities:
logging.info('Predicting locality type for "%s"' % loc.name)
key = 'loctype-%s' % loc.name
prediction = Cache.get(key)
if not prediction:
loctype, scores = self.predictor.get_type(loc.name)
prediction = dict(locname=loc.name, loctype=loctype, scores=scores)
Cache.put(key, prediction)
loc.type = prediction['loctype']
loc.type_scores = prediction['scores']
logging.info('Predicted "%s" for "%s"' % (loc.type, loc.name))
return localities
示例2: geocode
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
def geocode(self, localities):
for loc in localities:
loc.feature_geocodes = {}
loc.parts['feature_geocodes'] = {}
for feature in loc.parts['features']:
logging.info('Geocoding feature "%s"' % feature)
key = 'geocode-%s' % feature
geocode = Cache.get(key)
if not geocode:
geocode = self.geocoder.geocode(feature)
Cache.put(key, geocode)
loc.parts['feature_geocodes'][feature] = geocode
logging.info('Geocoded feature "%s"' % feature)
return localities
示例3: post
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
def post(self):
from user_sn import confirm
from team import get_team_for_team_leader
from cache import TYPE_EXTERN_FD, TYPE_LOCAL_FD
u = confirm(self)
team = get_team_for_team_leader(u)
from cache import Cache, CacheLocation
type = self.request.get("type")
if type=="INTERN":
ctype = TYPE_LOCAL_FD
elif type=="EXTERN":
ctype = TYPE_EXTERN_FD
where = CacheLocation.get(self.request.get("location"))
c = Cache(friendlyName = self.request.get("name"),type=ctype,last_touched=u,space_left=long(self.request.get("freespace")),permanent_location=where,checked_out=True)
c.put()
self.response.out.write("OK")
示例4: get
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
def get(self):
from cache import can_operate_on, ContentCopy, get_cache_by_name, Cache, TYPE_COMPUTER
from user_sn import confirm
u = confirm(self)
cname = self.request.get("cache")
logging.info("Syncing a cache named %s" % cname)
c = get_cache_by_name(cname)
if c == None:
logging.info("No cached named %s, making a new one" % cname)
c = Cache(friendlyName=cname, type=TYPE_COMPUTER, last_touched=u, space_left=-1, person_responsible=u)
c.put()
if not can_operate_on(c, u):
raise Exception("You're not permitted to sync this cache.")
# fetch everything that's "supposed" to be on the key
copies = ContentCopy.all().filter("where =", c).fetch(limit=1000)
self.response.out.write("OK\n")
for copy in copies:
self.response.out.write(copy.content.file_id + "\n")
示例5: TestAlgorithms
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class TestAlgorithms(unittest.TestCase):
def setUp(self):
pass
def test_algorithm(self, name=None):
if name == None:
return
self.cache = Cache(name, CACHESIZE)
self.assertEqual(str(self.cache.cache), name)
self.assertEqual(self.cache.get("trausti"), None)
self.assertEqual(self.cache.put("trausti", 100), 1)
self.assertEqual(self.cache.get("trausti"), 100)
for j in xrange(2000):
self.assertEqual(self.cache.put("%d" % j, 200), 1)
self.assertEqual(self.cache.get("1999"), 200)
for j in xrange(2000, 3000):
self.assertEqual(self.cache.get("%d" % j), None)
for j in xrange(NUMREQUESTS):
key = str(keydistribution[j])
if not self.cache.get(key):
self.cache.put(key, "A")
def test_LRU(self):
self.test_algorithm("LRU")
self.cache.cache.walk()
self.cache.put("hestur", 100)
d = self.cache.cache.get_stackdistance("hestur")
self.assertEqual(d[0], 0)
self.assertEqual(d[1], True)
self.cache.put("skinka", 101)
d = self.cache.cache.get_stackdistance("hestur")
self.assertEqual(d[0], 1)
self.assertEqual(d[1], True)
def test_CLOCK(self):
self.test_algorithm("CLOCK")
def test_LFU(self):
self.test_algorithm("LFU")
def test_LRU3(self):
self.test_algorithm("LRU3")
"""
示例6: ChainDb
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class ChainDb(object):
def __init__(self, settings, datadir, log, mempool, wallet, netmagic, readonly=False, fast_dbm=False):
self.settings = settings
self.log = log
self.mempool = mempool
self.wallet = wallet
self.readonly = readonly
self.netmagic = netmagic
self.fast_dbm = fast_dbm
self.blk_cache = Cache(500)
self.orphans = {}
self.orphan_deps = {}
# LevelDB to hold:
# tx:* transaction outputs
# misc:* state
# height:* list of blocks at height h
# blkmeta:* block metadata
# blocks:* block seek point in stream
self.blk_write = io.BufferedWriter(io.FileIO(datadir + '/blocks.dat','ab'))
self.blk_read = io.BufferedReader(io.FileIO(datadir + '/blocks.dat','rb'))
self.db = leveldb.LevelDB(datadir + '/leveldb')
try:
self.db.Get('misc:height')
except KeyError:
self.log.write("INITIALIZING EMPTY BLOCKCHAIN DATABASE")
batch = leveldb.WriteBatch()
batch.Put('misc:height', str(-1))
batch.Put('misc:msg_start', self.netmagic.msg_start)
batch.Put('misc:tophash', ser_uint256(0L))
batch.Put('misc:total_work', hex(0L))
self.db.Write(batch)
try:
start = self.db.Get('misc:msg_start')
if start != self.netmagic.msg_start: raise KeyError
except KeyError:
self.log.write("Database magic number mismatch. Data corruption or incorrect network?")
raise RuntimeError
def puttxidx(self, txhash, txidx, batch=None):
ser_txhash = ser_uint256(txhash)
try:
self.db.Get('tx:'+ser_txhash)
old_txidx = self.gettxidx(txhash)
self.log.write("WARNING: overwriting duplicate TX %064x, height %d, oldblk %064x, \
oldspent %x, newblk %064x" % (txhash, self.getheight(), old_txidx.blkhash, old_txidx.spentmask, txidx.blkhash))
except KeyError:
pass
batch = self.db if batch is not None else batch
batch.Put('tx:'+ser_txhash, hex(txidx.blkhash) + ' ' +
hex(txidx.spentmask))
return True
def getbalance(self, address):
balance = 0.0
txouts = self.listreceivedbyaddress(address)
for txout in txouts.itervalues():
balance = balance + txout['value']
return balance
"""
# scan the blocks for transactions to this address
chain_height = 10
for index in range(height, chain_height):
received, sent = scan_block(index, address)
balabce = balance + received - sent
if height < chain_height:
"""
def sendtoaddress(self, toaddress, amount):
tx = self.wallet.sendtoaddress(toaddress, amount)
self.mempool.add(tx)
def listreceivedbyaddress(self, address):
txouts = {}
end_height = self.getheight()
public_key_hash_hex = binascii.hexlify(utils.address_to_public_key_hash(address))
for height in xrange(end_height):
data = self.db.Get('height:' + str(height))
heightidx = HeightIdx()
heightidx.deserialize(data)
blkhash = heightidx.blocks[0]
block = self.getblock(blkhash)
for tx in block.vtx:
# if this transaction refers to this address in input, remove the previous transaction
for txin in tx.vin:
if not txin.scriptSig:
continue
script_key_hash_hex = binascii.hexlify(utils.scriptSig_to_public_key_hash(txin.scriptSig))
# print 'script_key_hash_hex: ', script_key_hash_hex
# print 'public_key_hash_hex: ', public_key_hash_hex
if script_key_hash_hex == public_key_hash_hex:
del txouts[txin.prevout.hash]
# if this transaction refers to this address in output, add this transaction
for n, txout in enumerate(tx.vout):
#.........这里部分代码省略.........
示例7: TestStatistics
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class TestStatistics(unittest.TestCase):
def setUp(self):
pass
def test_algorithm(self, name=None):
if name == None:
return
self.cache = Cache(name, CACHESIZE)
for j in xrange(CACHESIZE):
key = str(j)
self.cache.put(key, "A")
for j in xrange(CACHESIZE/2):
key = str(j)
self.cache.get(key)
self.assertEqual(self.cache.cache.stats.stats.hits, CACHESIZE/2)
self.assertEqual(self.cache.cache.stats.stats.requests, CACHESIZE/2)
for j in xrange(CACHESIZE/2):
key = str(j)
self.cache.get(key)
self.assertEqual(self.cache.cache.stats.stats.hits, CACHESIZE)
self.assertEqual(self.cache.cache.stats.stats.requests, CACHESIZE)
for j in xrange(CACHESIZE, 2*CACHESIZE):
key = str(j)
self.cache.get(key)
hits = self.cache.cache.stats.stats.hits
self.assertEqual(hits, CACHESIZE)
misses = self.cache.cache.stats.stats.misses
self.assertEqual(misses, CACHESIZE)
requests = self.cache.cache.stats.stats.requests
self.assertEqual(requests, 2*CACHESIZE)
self.cache.cache.stats.stats.make_pdf()
self.cache.cache.stats.ghostlist.make_pdf()
self.pdf = self.cache.cache.stats.stats.pdf
self.gpdf = self.cache.cache.stats.ghostlist.pdf
self.cdf = make_cdf(self.pdf, CACHESIZE, 1)
self.gcdf = make_cdf(self.gpdf, CACHESIZE, 1) # The default size of the ghostlist is the same as the cache
self.assertTrue((self.cdf[CACHESIZE] - hits) < 1e-5) # the number of hits
self.assertEqual(self.gcdf[0], 0) # no extra hits in the ghostlist!
self.assertEqual(self.gcdf[CACHESIZE-1], 0) # no extra hits in the ghostlist!
def test_LRU(self):
self.test_algorithm("LRU")
def test_CLOCK(self):
self.test_algorithm("CLOCK")
def test_LFU(self):
self.test_algorithm("LFU")
def test_LRU3(self):
self.test_algorithm("LRU3")
"""
def test_LRU10(self):
self.test_algorithm("LRU10")
"""
"""
示例8: ChainDb
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class ChainDb(object):
def __init__(self, settings, datadir, mempool, wallet, vaultdb, netmagic, readonly=False, fast_dbm=False):
self.settings = settings
self.mempool = mempool
self.wallet = wallet
self.vaultdb = vaultdb
#FIXME: set this value from genesis.dat
self.difficulty = 0x1f00ffff # Initial difficulty
self.readonly = readonly
self.netmagic = netmagic
self.fast_dbm = fast_dbm
self.blk_cache = Cache(500)
self.orphans = {}
self.orphan_deps = {}
self.logger = logging.getLogger(__name__)
# LevelDB to hold:
# tx:* transaction outputs
# misc:* state
# height:* list of blocks at height h
# blkmeta:* block metadata
# blocks:* block seek point in stream
self.blk_write = io.BufferedWriter(io.FileIO(datadir + '/blocks.dat','ab'))
self.blk_read = io.BufferedReader(io.FileIO(datadir + '/blocks.dat','rb'))
self.db = leveldb.LevelDB(datadir + '/leveldb')
try:
self.db.Get('misc:height')
except KeyError:
self.logger.debug("INITIALIZING EMPTY BLOCKCHAIN DATABASE")
batch = leveldb.WriteBatch()
batch.Put('misc:height', str(-1))
batch.Put('misc:msg_start', self.netmagic.msg_start)
batch.Put('misc:tophash', ser_uint256(0L))
batch.Put('misc:total_work', hex(0L))
self.db.Write(batch)
try:
start = self.db.Get('misc:msg_start')
if start != self.netmagic.msg_start: raise KeyError
except KeyError:
self.self.logger.error("Database magic number mismatch. Data corruption or incorrect network?")
raise RuntimeError
def puttxidx(self, txhash, txidx, batch=None):
self.logger.debug("Put txidx: %064x " % txhash)
ser_txhash = ser_uint256(txhash)
try:
self.db.Get('tx:'+ser_txhash)
old_txidx = self.gettxidx(txhash)
self.logger.warning("Overwriting duplicate TX %064x, height %d, oldblk %064x, \
oldspent %x, newblk %064x" % (txhash, self.getheight(), old_txidx.blkhash, \
old_txidx.spentmask, txidx.blkhash))
except KeyError:
pass
batch = self.db if batch is not None else batch
batch.Put('tx:'+ser_txhash, hex(txidx.blkhash) + ' ' +
hex(txidx.spentmask))
return True
def gettxidx(self, txhash):
self.logger.debug("Get txidx: %064x" % txhash)
ser_txhash = ser_uint256(txhash)
try:
ser_value = self.db.Get('tx:'+ser_txhash)
except KeyError:
self.logger.error('DB KEY Error: %064x' % txhash)
return None
pos = string.find(ser_value, ' ')
txidx = TxIdx()
txidx.blkhash = long(ser_value[:pos], 16)
txidx.spentmask = long(ser_value[pos+1:], 16)
return txidx
def gettx(self, txhash):
txidx = self.gettxidx(txhash)
if txidx is None:
self.logger.debug('tdidx is None')
return None
block = self.getblock(txidx.blkhash)
for tx in block.vtx:
tx.calc_sha256()
if tx.sha256 == txhash:
return tx
self.logger.error("ERROR: Missing TX %064x in block %064x" % \
(txhash, txidx.blkhash))
return None
def getsavings(self, vault):
savings = 0.0
#.........这里部分代码省略.........
示例9: Database
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class Database(object):
def __init__(self, multi_value=False, cache_port=None, router=None, domain=None):
self._db = CommonDB(name=self.name, router=router, domain=domain)
self._multi_value = multi_value
if cache_port != None:
self._cache = Cache(cache_port)
else:
self._cache = None
@property
def name(self):
return self.__class__.__name__.lower()
def get(self, key, first=False):
val = None
cache = False
if self._cache:
cache = True
if not self._multi_value or first:
val = self._cache.get(key)
if not val:
coll = self._db.collection(key)
conn = self._db.connection(coll)
val = self._db.get(conn, key)
if self._multi_value:
if first and type(val) == list:
val = val[0]
else:
cache = False
if cache:
self._cache.put(key, val)
return val
def put(self, key, value):
if self._cache:
self._cache.delete(key)
if not self._multi_value:
val = {'$set':{VAL_NAME:value}}
else:
val = {'$addToSet':{VAL_NAME:value}}
coll = self._db.collection(key)
conn = self._db.connection(coll)
self._db.put(conn, key, val, create=True)
def delete(self, key, value=None, regex=False):
if self._cache:
self._cache.delete(key)
coll = self._db.collection(key)
conn = self._db.connection(coll)
if not value:
self._db.delete(conn, key)
else:
if not self._multi_value:
log_err(self, 'failed to delete')
raise Exception(log_get(self, 'failed to delete'))
if not regex:
self._db.put(conn, key, {'$pull':{VAL_NAME:value}})
else:
self._db.put(conn, key, {'$pull':{VAL_NAME:{'$regex':value}}})
示例10: MapsProxy
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
class MapsProxy(object):
def __init__(self):
self.cache = Cache()
@cherrypy.expose
@cherrypy.tools.json_out()
def index(self):
return {'status': 'ok', 'service': 'mapper/proxy'}
def transform_maps_response(self, response):
route = response['routes'][0]
start_point = route['legs'][0]['start_location']
end_point = route['legs'][-1]['end_location']
distance = route['distance']
duration = route['duration']
pass
def p_transit(self, src, tar, arrival_time=None, departure_time=None):
if arrival_time is None and departure_time is None:
return {
'error': 'Either arrival_time or departure_time must be provided'
}
url = conf.transit_url % (src, tar)
if arrival_time:
url = '%s&arrival_time=%s' % (url, arrival_time)
else:
url = '%s&departure_time=%s' % (url, departure_time)
cached = self.cache.get(url)
if cached:
return cached
r = requests.get(url)
if r.status_code == 200:
j = r.json()
for r in j['routes']:
total = 0
for l in r['legs']:
total += l['duration']['value']
r['total_length'] = total
self.cache.put(url, j)
return j
else:
return r.json()
def p_biking(self, src, tar, arrival_time=None, departure_time=None):
if arrival_time is None and departure_time is None:
return {
'error': 'Either arrival_time or departure_time must be provided'
}
url = conf.bike_url % (src, tar)
if arrival_time:
url = '%s&arrival_time=%s' % (url, arrival_time)
else:
url = '%s&departure_time=%s' % (url, departure_time)
cached = self.cache.get(url)
if cached:
return cached
r = requests.get(url)
if r.status_code == 200:
j = r.json()
for r in j['routes']:
total = 0
for l in r['legs']:
total += l['duration']['value']
r['total_length'] = total
self.cache.put(url, j)
return j
else:
return r.json()
def p_driving(self, src, tar, arrival_time=None, departure_time=None):
if arrival_time is None and departure_time is None:
return {
'error': 'Either arrival_time or departure_time must be provided'
}
url = conf.driving_url % (src, tar)
if arrival_time:
url = '%s&arrival_time=%s' % (url, arrival_time)
else:
url = '%s&departure_time=%s' % (url, departure_time)
cached = self.cache.get(url)
if cached:
return cached
r = requests.get(url)
if r.status_code == 200:
j = r.json()
for r in j['routes']:
total = 0
for l in r['legs']:
#.........这里部分代码省略.........
示例11: run_multi
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import put [as 别名]
def run_multi(self, queries, cached=False):
"""
Function: run_multi
-------------------
Runs multiple SQL statements at once.
"""
# Consume old results if needed.
[row for row in self.cursor]
sql_list = split(queries)
# Consume any additional result-sets that might have been left
# on the connection.
# try:
# while self.cursor.nextset():
# pass
# except Error:
# pass
result = Result()
for sql in sql_list:
sql = sql.rstrip().rstrip(";")
if len(sql) == 0:
continue
query_results = Cache.get(sql)
# Results are not to be cached or are not in the cache and needs to
# be cached. Run the query.
if not query_results or not cached:
try:
self.clear_cursor()
self.cursor.execute(sql)
# except DatabaseError as e:
# if 'already exists' in str(e):
# print("[warning: %s]" % str(e))
# else:
# # Reraise the exception
# raise e
# If the query times out.
except mysql.connector.errors.OperationalError as e:
raise TimeoutError(e)
# If something is wrong with their query.
except mysql.connector.errors.ProgrammingError as e:
if 'already exists' in str(e):
log("[warning: %s]" % str(e))
else:
raise DatabaseError(e)
# If the query can't be run as a single query, attempt to do it with a
# multi-line query.
except mysql.connector.errors.Error as e:
print("ERROR while executing SQL: %s" % sql)
print(str(e))
raise DatabaseError(e)
query_results = self.get_results()
if cached:
Cache.put(sql, query_results)
result = query_results
# If no longer in a transaction, remove all savepoints.
if not self.db.in_transaction:
self.savepoints = []
return result