当前位置: 首页>>代码示例>>Python>>正文


Python Cache.read方法代码示例

本文整理汇总了Python中cache.Cache.read方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.read方法的具体用法?Python Cache.read怎么用?Python Cache.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cache.Cache的用法示例。


在下文中一共展示了Cache.read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import read [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)
开发者ID:imclab,项目名称:plotdevice-libs,代码行数:35,代码来源:urbandictionary.py

示例2: __init__

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import read [as 别名]
class LinkedMarkMail:

    def __init__(self, base="http://linkedmarkmail.wikier.org", log="linkedmarkmail.log"):
        self.base = base
        self.api = MarkMail("http://markmail.org")
        self.cache = Cache()
        self.cache.register(Post, "message-%s.rdf")
        self.cache.register(Thread, "thread-%s.rdf")
        logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", filename=log)
        logging.info("Created a new instance of LinkedMarkMail at %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

    def search(self, query):
        search = self.api.search(query)
        return "" #FIXME

    def get_message(self, key):
        if (self.cache.is_cached(key, Post)):
            logging.info("Recovering message %s from cache..." % key)
            return self.cache.read(key, Post)
        else:
            logging.info("Trying to get message %s from MarkMail..." % key)
            message = self.api.get_message(key)
            if (message != None):
                url = "%s/message/%s" % (self.base, key)
                post = Post(url, key, message["title"], message["content"])
                triples = len(post)
                #if (not self.cache.is_cached(post.get_key(), post.__class__)):
                #    self.cache.write(post)
                #    logging.info("Updated cache of post %s (%d triples)" % (key, triples))
                logging.info("Returning %d triples of post %s" % (triples, key))
                return post.get_data_xml()
            else:
                logging.error("Post %s not found" % key)
                return None

    def get_thread(self, key):
        logging.info("Trying to get thread %s" % key)
        thread = self.api.get_thread(key)
        if (thread != None):
            siocThread = Thread(self.base, key, thread["subject"], thread["permalink"], thread["atomlink"], thread["messages"]["message"])
            triples = len(siocThread)
            if (self.cache.is_dirty(siocThread)):
                self.cache.update(siocThread)
                logging.info("Updated cache of thread %s (%d triples)" % (key, triples))
            logging.info("Returning %d triples of thread %s" % (triples, key))
            return siocThread.get_data_xml()
        else:
            logging.error("Thread %s not found" % key)
            return None
开发者ID:BackupTheBerlios,项目名称:swaml-svn,代码行数:51,代码来源:linkedmarkmail.py

示例3: Processor

# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import read [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:
#.........这里部分代码省略.........
开发者ID:hendychua,项目名称:cachecoherence,代码行数:103,代码来源:processor.py


注:本文中的cache.Cache.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。