当前位置: 首页 > wzjs >正文

厦门建设银行网站如何建设电子商务网站

厦门建设银行网站,如何建设电子商务网站,软装设计师资格证,做盗版电影网站违法吗以下是一个基于Python实现的缓存淘汰替换策略算法,该算法将缓存分区,并根据不同分区的优先级进行淘汰,同时会自适应地调整缓存汰换的时机和力度,还会与GPTCache自带的LRU和FIFO策略进行对比。 import time from gptcache.manager…

以下是一个基于Python实现的缓存淘汰替换策略算法,该算法将缓存分区,并根据不同分区的优先级进行淘汰,同时会自适应地调整缓存汰换的时机和力度,还会与GPTCache自带的LRU和FIFO策略进行对比。

import time
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.processor.pre import get_prompt
from gptcache.adapter.api import init_similar_cache
from gptcache.strategy import LRUCacheStrategy, FIFOCacheStrategy# 定义缓存类
class CustomCache:def __init__(self, capacity):self.capacity = capacityself.high_priority = {}self.hot_window = {}self.medium_priority = {}self.other_cache = {}self.total_count = 0def add(self, key, value, category):# 初始化缓存项字段cache_item = {'value': value,'freshness': 1.0,'activity': 1,'timestamp': time.time(),'eviction_rate': 0.0,'category': category}self.total_count += 1# 根据类别分配到不同分区if category == 'high':self.high_priority[key] = cache_itemelif category == 'hot':self.hot_window[key] = cache_itemelif category == 'medium':self.medium_priority[key] = cache_itemelse:self.other_cache[key] = cache_item# 检查是否需要淘汰if self.total_count > self.capacity:self.evict()def get(self, key):for cache in [self.high_priority, self.hot_window, self.medium_priority, self.other_cache]:if key in cache:cache[key]['activity'] += 1cache[key]['timestamp'] = time.time()cache[key]['freshness'] = 1.0return cache[key]['value']return Nonedef evict(self):# 自适应调整淘汰时机和力度# 这里简单模拟根据当前时间进行调整,实际可根据季节性或周期性数据调整current_time = time.time()if current_time % 3600 < 1800:  # 假设每小时前半段淘汰力度大eviction_count = 2else:eviction_count = 1while self.total_count > self.capacity and eviction_count > 0:if self.other_cache:key = next(iter(self.other_cache))del self.other_cache[key]elif self.medium_priority:key = next(iter(self.medium_priority))del self.medium_priority[key]elif self.hot_window:# 淘汰热点窗口中活跃度最低的项min_activity_key = min(self.hot_window, key=lambda k: self.hot_window[k]['activity'])del self.hot_window[min_activity_key]elif self.high_priority:# 淘汰高优先级中最旧的项oldest_key = min(self.high_priority, key=lambda k: self.high_priority[k]['timestamp'])del self.high_priority[oldest_key]self.total_count -= 1eviction_count -= 1# 对比测试函数
def compare_strategies():capacity = 10# 自定义策略custom_cache = CustomCache(capacity)# LRU策略lru_strategy = LRUCacheStrategy(capacity)lru_cache_base = CacheBase('sqlite')lru_vector_base = VectorBase('faiss', dimension=128)lru_data_manager = get_data_manager(lru_cache_base, lru_vector_base, lru_strategy)init_similar_cache(data_manager=lru_data_manager, pre_embedding_func=get_prompt)# FIFO策略fifo_strategy = FIFOCacheStrategy(capacity)fifo_cache_base = CacheBase('sqlite')fifo_vector_base = VectorBase('faiss', dimension=128)fifo_data_manager = get_data_manager(fifo_cache_base, fifo_vector_base, fifo_strategy)init_similar_cache(data_manager=fifo_data_manager, pre_embedding_func=get_prompt)# 模拟数据访问data = [(i, f"value_{i}", 'high' if i < 3 else 'medium' if i < 6 else 'hot' if i < 8 else 'other') for i in range(20)]custom_hits = 0lru_hits = 0fifo_hits = 0for key, value, category in data:# 自定义策略if custom_cache.get(key) is None:custom_cache.add(key, value, category)else:custom_hits += 1# LRU策略if lru_data_manager.get_data(key, None) is None:lru_data_manager.save(key, value, None, None)else:lru_hits += 1# FIFO策略if fifo_data_manager.get_data(key, None) is None:fifo_data_manager.save(key, value, None, None)else:fifo_hits += 1print(f"Custom Cache Hits: {custom_hits}")print(f"LRU Cache Hits: {lru_hits}")print(f"FIFO Cache Hits: {fifo_hits}")if __name__ == "__main__":compare_strategies()

代码说明:

  1. CustomCache类

    • __init__:初始化缓存分区和容量。
    • add:将缓存项添加到相应的分区,并在需要时调用evict方法进行淘汰。
    • get:根据键查找缓存项,并更新其活跃度、时间戳和新鲜度。
    • evict:根据分区优先级进行淘汰,同时自适应地调整淘汰的时机和力度。
  2. compare_strategies函数

    • 初始化自定义缓存、LRU缓存和FIFO缓存。
    • 模拟数据访问,记录每个缓存的命中次数。
    • 输出每个缓存的命中次数。

注意事项:

  • 代码中使用了GPTCache库,需要确保已经安装了该库。
  • 自适应调整淘汰时机和力度的逻辑可以根据实际需求进行修改,这里只是简单模拟了每小时前半段淘汰力度大的情况。

文章转载自:

http://2zZ14UTx.xxzjb.cn
http://NLCIFJDh.xxzjb.cn
http://XzaoDpMv.xxzjb.cn
http://fLDYWFSt.xxzjb.cn
http://LrreP2Yh.xxzjb.cn
http://X0x4NEdR.xxzjb.cn
http://05BhoISn.xxzjb.cn
http://kMiEOx3H.xxzjb.cn
http://aOrKyteO.xxzjb.cn
http://E5kGtvMD.xxzjb.cn
http://RtpMDcH0.xxzjb.cn
http://iLeElH7o.xxzjb.cn
http://ghZ4rosm.xxzjb.cn
http://abo8Pj8K.xxzjb.cn
http://PsiHwiJ4.xxzjb.cn
http://PRd9mFi7.xxzjb.cn
http://U8KgjQE2.xxzjb.cn
http://kSKBtayJ.xxzjb.cn
http://jphuW3T9.xxzjb.cn
http://y3FLIQ13.xxzjb.cn
http://UTynjq1r.xxzjb.cn
http://6FrgXPDS.xxzjb.cn
http://K2gRXA3H.xxzjb.cn
http://cZCnz4bF.xxzjb.cn
http://4I71aRk6.xxzjb.cn
http://cY2HRnzP.xxzjb.cn
http://NgCUwTvL.xxzjb.cn
http://owdqnDXR.xxzjb.cn
http://h9ugDgRJ.xxzjb.cn
http://Z2NYdW1K.xxzjb.cn
http://www.dtcms.com/wzjs/671701.html

相关文章:

  • 网站footer模板开发一款手机app软件需要多少钱
  • 飞鸿网架公司优化大师官方免费下载
  • 凡科网网站建设一个空间做2个网站
  • 哪里有服务好的网站建设公司济宁建网站公司
  • 中原郑州网站建设wordpress如何安装模板
  • 阿里云搭建企业网站网站怎么分工做
  • 深圳高端网站建设价格wordpress加上特效
  • 网站开发技术大学教材惠州网站建设 鑫
  • 学校网站的建设目标是什么学编程先学什么
  • 《php网站开发》课程资料网站的ns记录
  • 自己做网站能否赚钱红花岗区住房和城乡建设局网站
  • 制作小程序网站源码破解php网站后台账号密码
  • 网站建设结束语asp做网站步骤
  • 企业网站不付服务费应该怎么做吴江住宅城乡建设局网站
  • 衡水网站优化线上网络平台推广
  • 邢台做移动网站的地方龙岩推广公司
  • 培训网站官网html静态网页制作成品
  • 潍坊点睛做网站怎么样网站建设用阿里云的虚拟主机
  • 大兴安岭网站推广霸气又聚财的名字大全
  • 广州网站开发设计平台给公司建立网站不可以做到的
  • 科技企业网站建设模板使用c 语言建设网站优点
  • 珠海市网站开发公司电话知识付费微网站开发
  • 设计网站实现PDF在线阅读需要怎么做鸿邑科技 网站建设
  • 网站动图是怎么做的社区网站建设论文
  • 佛山网站建设联系c语言软件开发和网站开发区别
  • 水果电子商务网站建设规划书手机域名解析错误怎么解决
  • 网站负责人半身照wordpress 网站地址
  • 竞价恶意点击器网站优化 情况
  • 珠海做网站专业公司网站备案号查电话号码
  • 个人电脑做网站服务器镇江属于哪里