本文整理汇总了Python中cache.Cache.write方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.write方法的具体用法?Python Cache.write怎么用?Python Cache.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.write方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
def __init__(self, q, cached=True):
url = "http://api.urbandictionary.com/soap"
key = "91cf66fb7f14bbf7fb59c7cf5e22155f"
# Live connect for uncached queries
# or queries we do not have in cache.
cache = Cache("urbandictionary", ".pickle")
if not cached or not cache.exists(q):
server = soap.SOAPProxy(url)
definitions = server.lookup(key, q)
data = []
for item in definitions:
ubd = UrbanDictionaryDefinition(
item.word, item.url, item.definition, item.example, item.author
)
self.append(ubd)
data.append( [item.word, item.word, item.definition, item.example, item.author] )
# Cache a pickled version of the response.
if cached:
data = pickle.dumps(data)
cache.write(q, data)
# For cached queries,
# unpack the pickled version in the cache.
else:
definitions = cache.read(q)
definitions = pickle.loads(definitions)
for item in definitions:
ubd = UrbanDictionaryDefinition(
item[0], item[1], item[2], item[3], item[4]
)
self.append(ubd)
示例2: syncCache
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
def syncCache():
cache1 = Cache(GIT_DIR)
cache1.start()
cache2 = Cache(GIT_DIR)
cache2.initial()
for path in cache2.list():
if not cache1.contains(path):
cache1.update(path)
if not isdir(join(CC_DIR, path.file)):
copy(path.file)
cache1.write()
示例3: testLoad
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
def testLoad(self):
dir = tempfile.mkdtemp()
f = open(join(dir, cache.FILE), 'w')
f.write(TEMP1)
f.close()
try:
c = Cache(dir)
self.assertFalse(c.isChild(CCFile('file.py', '/main/a/1')))
self.assertFalse(c.isChild(CCFile('file.py', r'\main\a\1')))
self.assertTrue(c.isChild(CCFile('file.py', '/main/a/b/c/1')))
self.assertFalse(c.isChild(CCFile('file.py', '/main/a/c/1')))
c.update(CCFile('file.py', '/main/a/b/2'))
c.update(CCFile('file2.py', '/main/c/2'))
c.write()
f = open(join(dir, cache.FILE), 'r')
try:
self.assertEqual(TEMP1_EXPECTED, f.read())
finally:
f.close()
finally:
shutil.rmtree(dir)
示例4: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
def __init__(self, q, cached=True):
url = "http://api.urbandictionary.com/soap?wsdl"
key = "91cf66fb7f14bbf7fb59c7cf5e22155f"
# Live connect for uncached queries
# or queries we do not have in cache.
cache = Cache("urbandictionary", ".pickle")
if not cached or not cache.exists(q):
server = soap.SOAPProxy(url)
try:
definitions = server.lookup(key, q)
except Exception, soap.faultType:
raise UrbanDictionaryError, "the API is no longer supported"
data = []
for item in definitions:
ubd = UrbanDictionaryDefinition(item.word, item.url, item.definition, item.example, item.author)
self.append(ubd)
data.append([item.word, item.word, item.definition, item.example, item.author])
# Cache a pickled version of the response.
if cached:
data = pickle.dumps(data)
cache.write(q, data)
示例5: __init__
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
class TestCache:
def __init__(self):
self.cache = Cache(SIZE)
def test_read_write(self):
# basic write and read
self.cache.write("test_key", "test_value")
assert "test_value" == self.cache.beginRead("test_key")
# beginRead: read key not present
try:
self.cache.beginRead("key_not_present")
assert False
except:
assert True
# endRead: read key not present
try:
self.cache.endRead("key_not_present")
assert False
except:
assert True
def test_eviction(self):
""" To check an entry is never evicted once beginRead is called """
self.cache.write("test_key", "test_value")
value = self.cache.beginRead("test_key")
for i in range(TEST_SIZE):
self.cache.write(i, i)
try:
# if evicted, endRead should throw and exception
self.cache.endRead("test_key")
assert True
except:
assert False
def test_size(self):
""" size of hash_db should never exceed specified size of cache """
# size
for i in range(TEST_SIZE):
self.cache.write(i, i)
assert len(self.cache.hash_db) <= SIZE
示例6: Processor
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import write [as 别名]
class Processor(object):
INTERESTED = 1
NOT_INTERESTED = 0
NOT_STALLED = 0
STALLED = 1
NO_BUS = 0
BUS_READ = 1
BUS_READ_EXCLUSIVE = 2
def __init__(self, identifier, protocol="MESI", associativity=1, block_size=64, cache_size=4096):
self.cache = Cache(associativity=associativity, block_size=block_size, cache_size=cache_size)
self.cycles = 0
self.latency = 0
self.bus_transactions_count = [0, 0] # BUS_READ & BUS_READ_EXCLUSIVE respectively
self.identifier = identifier
self.protocol = protocol
self.log = logging.getLogger("p"+str(identifier))
self.stall_status = self.NOT_STALLED
def check_for_bus_transaction_needed(self, instruction):
instruction_type, address, count = instruction
instruction_type = int(instruction_type)
if self.protocol.upper() == "MESI":
if instruction_type == READ_MEMORY:
ret = self.cache.is_address_present(address, update_hits_misses_count=True)
if ret == self.cache.CACHE_MISS:
self.bus_transactions_count[0] += 1
return (self.BUS_READ, address)
else:
return (self.NO_BUS, address)
elif instruction_type == WRITE_MEMORY:
ret = self.cache.is_address_present(address, update_hits_misses_count=True)
if ret == self.cache.CACHE_MISS:
self.bus_transactions_count[1] += 1
return (self.BUS_READ_EXCLUSIVE, address)
elif ret == self.cache.CACHE_HIT_MODIFIED:
return (self.NO_BUS, address)
elif ret == self.cache.CACHE_HIT_EXCLUSIVE:
return (self.NO_BUS, address)
elif ret == self.cache.CACHE_HIT_SHARED:
self.bus_transactions_count[1] += 1
return (self.BUS_READ_EXCLUSIVE, address)
elif self.protocol.upper() == "DRAGON":
#TODO: DRAGON
pass
def execute(self, instruction, read_type="S"):
# when the execute function is called, it will check if there are
# any latencies. if self.latency != 0, self.latency will decrease by
# 1 cycle and instruction won't be executed. If self.latency == 0,
# the instruction will be executed.
self.cycles += 1
if self.latency > 0:
self.latency -= 1
if self.latency == 0:
self.stall_status = self.NOT_STALLED
else:
self.stall_status = self.STALLED
else:
instruction_type, address, count = instruction
instruction_type = int(instruction_type)
# no need since we are not executing fetch instructions
# if instruction_type == FETCH_INSTRUCTION:
# self.stall_status = self.NOT_STALLED
if instruction_type == READ_MEMORY:
ret = self.cache.is_address_present(address)
if ret == self.cache.CACHE_MISS:
self.cache.read(address, read_type)
self.latency = 10
self.stall_status = self.STALLED
else:
self.cache.read(address, read_type, update_lru_only=True)
self.stall_status = self.NOT_STALLED
elif instruction_type == WRITE_MEMORY:
ret = self.cache.is_address_present(address)
if ret == self.cache.CACHE_MISS:
self.cache.write(address)
self.latency = 10
self.stall_status = self.STALLED
else:
self.cache.write(address, update_lru_only=True)
self.stall_status = self.NOT_STALLED
def snoop(self, bus_transaction_type, address):
if self.protocol.upper() == "MESI":
index = -1
set_index = int((math.floor(address/self.cache.block_size)) % len(self.cache.sets))
set_to_search = self.cache.sets[set_index]
for i, block in enumerate(set_to_search):
if block is not None and address in block.words and block.state in MES:
index = i
break
if index > -1:
#.........这里部分代码省略.........