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

达州纪委党风廉政建设网站做一个公司网页多少钱

达州纪委党风廉政建设网站,做一个公司网页多少钱,iis网站wordpress登陆,视频网站建设方案以下是一个基于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://www.dtcms.com/wzjs/210997.html

相关文章:

  • 网站开发研究资料书籍百度推广一个月多少钱
  • jsporacle动态网站开发企业如何网络推广
  • 360免费建站李梦网络营销主要是什么
  • 1688域名网站哪里有学电脑培训班
  • 如何做网站收录搜索量最大的关键词
  • 网站后台编辑器下载上海seo优化
  • 青浦做网站公司注册
  • 合肥网站建设新手网店运营工作内容
  • 济南住建局官方网站网络优化是做啥的
  • 小企业网站制作深圳seo推广
  • 不用写代码可以做网站的软件怎么样推广最有效最快速
  • 自我介绍网页郑州靠谱seo整站优化
  • 河南做网站多少钱域名注册网站系统
  • 服装网站建设效果2023年8月新闻热点事件
  • 买了虚拟主机怎么建设网站表白网页制作免费网站制作
  • 企业网络组网设计方案seo的优缺点
  • 微信上的网站怎么做的吗关键词seo排名公司
  • 摄影作品网站建设方案书网站设计方案模板
  • 网站建设制作放之宁波seo推广服务
  • 关于加强政务网站建设的通知常见的网络营销平台有哪些
  • 电子商务网站b2c开源网站怎么注册个人网站
  • 网站建设后的优势四川seo推广方案
  • 群晖nas安装wordpress南宁seo推广外包
  • 欧亚达网站是哪家公司做的郑州网站优化培训
  • 怎么做网站备案八百客crm系统登录入口
  • 网页设计基础填空题及答案石家庄网络推广优化
  • 做网站 服务器多少钱一年深圳做网站公司
  • 个人网站示例营销推广是干什么的
  • 免费的产品展示小程序seo工具有哪些
  • 新疆做网站的公司排名营销推广的工具有哪些