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

杭州做网站的谷歌竞价推广教程

杭州做网站的,谷歌竞价推广教程,web前端模板下载网站,南宁建设网站培训这个类会在后台自动更新缓存数据,你只需要调用方法来获取数据即可。 自动更新缓存类 以下是 AutoUpdatingCache 类的实现: import threading import timeclass AutoUpdatingCache:def __init__(self, update_function, expiry_time60):""&qu…

这个类会在后台自动更新缓存数据,你只需要调用方法来获取数据即可。


自动更新缓存类

以下是 AutoUpdatingCache 类的实现:

import threading
import timeclass AutoUpdatingCache:def __init__(self, update_function, expiry_time=60):"""初始化缓存类。:param update_function: 一个函数,用于生成或更新缓存数据。:param expiry_time: 缓存的更新周期(秒)。"""self.update_function = update_functionself.expiry_time = expiry_timeself.cache_data = Noneself.last_updated = 0self.lock = threading.Lock()self._start_background_update()def _start_background_update(self):# 启动后台线程更新缓存self.update_thread = threading.Thread(target=self._update_cache_periodically)self.update_thread.daemon = Trueself.update_thread.start()def _update_cache_periodically(self):while True:current_time = time.time()if current_time - self.last_updated >= self.expiry_time:self._update_cache()time.sleep(1)  # 每秒检查一次def _update_cache(self):with self.lock:try:print("Updating cache...")new_data = self.update_function()self.cache_data = new_dataself.last_updated = time.time()print("Cache updated!")except Exception as e:print(f"Error updating cache: {e}")def get_data(self):with self.lock:if self.cache_data is not None:return self.cache_dataelse:return "Cache is initializing, please try again later."

使用说明

  1. 定义一个数据生成函数

    首先,需要定义一个用于生成或更新缓存数据的函数。这个函数可以是任何耗时的操作,例如从数据库查询、计算复杂结果等。

    import timedef generate_cache_data():# 模拟耗时操作time.sleep(5)return {"value": "fresh data", "timestamp": time.time()}
    
  2. 创建缓存类的实例

    将数据生成函数传递给 AutoUpdatingCache 类,并设置缓存更新周期。

    cache = AutoUpdatingCache(update_function=generate_cache_data, expiry_time=30)
    
  3. 获取缓存数据

    在需要的地方调用 get_data() 方法即可获取缓存数据。

    data = cache.get_data()
    print(data)
    

完整示例

将以上步骤组合起来:

import threading
import timeclass AutoUpdatingCache:def __init__(self, update_function, expiry_time=60):self.update_function = update_functionself.expiry_time = expiry_timeself.cache_data = Noneself.last_updated = 0self.lock = threading.Lock()self._start_background_update()def _start_background_update(self):self.update_thread = threading.Thread(target=self._update_cache_periodically)self.update_thread.daemon = Trueself.update_thread.start()def _update_cache_periodically(self):while True:current_time = time.time()if current_time - self.last_updated >= self.expiry_time:self._update_cache()time.sleep(1)def _update_cache(self):with self.lock:try:print("Updating cache...")new_data = self.update_function()self.cache_data = new_dataself.last_updated = time.time()print("Cache updated!")except Exception as e:print(f"Error updating cache: {e}")def get_data(self):with self.lock:if self.cache_data is not None:return self.cache_dataelse:return "Cache is initializing, please try again later."# 数据生成函数
def generate_cache_data():time.sleep(5)  # 模拟耗时操作return {"value": "fresh data", "timestamp": time.time()}# 创建缓存实例
cache = AutoUpdatingCache(update_function=generate_cache_data, expiry_time=30)# 模拟获取数据
for _ in range(10):data = cache.get_data()print(data)time.sleep(10)

代码解释

  • AutoUpdatingCache 类

    • init 方法:
      • 初始化缓存,设置数据生成函数和缓存更新周期。
      • 启动后台线程 _update_cache_periodically
    • _update_cache_periodically 方法:
      • 无限循环,每隔一秒检查缓存是否需要更新。
      • 如果当前时间距离上次更新时间超过了 expiry_time,则调用 _update_cache
    • _update_cache 方法:
      • 使用 update_function 更新缓存数据。
      • 使用锁机制 threading.Lock 确保线程安全。
    • get_data 方法:
      • 获取缓存数据。
      • 如果缓存数据为空(初始化中),返回提示信息。
  • 数据生成函数

    • generate_cache_data 函数模拟一个耗时操作,生成新的缓存数据。
  • 使用示例

    • 创建缓存实例并在循环中每隔 10 秒获取一次数据,观察缓存的更新情况。

注意事项

  • 线程安全:

    • 使用 threading.Lock 确保在多线程环境下数据访问的安全性。
  • 异常处理:

    • 在更新缓存时,捕获可能的异常,防止线程崩溃。
  • 后台线程:

    • 将线程设置为守护线程(daemon=True),使得主程序退出时,线程自动结束。

应用场景

你可以将这个缓存类应用在 Web 应用程序中,例如在 Sanic 的路由中:

from sanic import Sanic
from sanic.response import jsonapp = Sanic("CacheApp")@app.route("/data")
async def get_cached_data(request):data = cache.get_data()return json({"data": data})if __name__ == "__main__":# 确保缓存在应用启动前初始化cache = AutoUpdatingCache(update_function=generate_cache_data, expiry_time=30)app.run(host="0.0.0.0", port=8000)

这样,用户在访问 /data 路由时,总是能得到缓存中的数据,而缓存会在后台自动更新,不会因为更新缓存而导致请求超时。


😊

http://www.khdw.cn/news/19853.html

相关文章:

  • 网站建设的前期工作基础买淘宝店铺多少钱一个
  • 怎么选择一家好的网站建设公司ui设计培训班哪家好
  • 网站数据库备份还原太原seo自媒体
  • 长春建站企业产品宣传推广方案
  • 巴中市建设局网站怎么在百度发布免费广告
  • 优服优科网站建设公司自己建网站要多少钱
  • 妇女之家网站建设方案贴吧高级搜索
  • 高校图书馆网站建设google推广平台怎么做
  • 东莞活动网站设计模板淮北网站建设
  • 做网站前需要做什么准备推广普通话手抄报简单漂亮
  • 设计教育培训seo基础培训
  • 淘宝店铺装网站导航怎么做无线网络优化是做什么的
  • 在日常网站建设中宣传积极网络营销推广策划案例
  • 站内免费推广免费视频网站推广软件
  • 给小孩做辅食的网站阿里云域名注册
  • 健身俱乐部网站建设方案设计百度推广怎么推广
  • 怎么把自己的网站放到百度上保定网站推广公司
  • 石家庄政府网站建设品牌型网站制作价格
  • 无锡优化网站排名设计培训学院
  • 下厨房网站学做蒸包视频今日头条网页版入口
  • 网站建设图百度一下百度一下你就知道
  • 织梦网站怎么做伪静态百度公司排名
  • 网络营销战略的内容seo是搜索引擎营销
  • 电脑做网站软件网址大全下载到桌面
  • 建筑工地网站有哪些最好用的磁力搜索神器
  • 网站后台栏目根据什么做的企业网站推广建议
  • wordpress关注公众号天津seo顾问
  • 厦门集美区网站建设百度关键词统计
  • wordpress授权代码百度关键词排名优化
  • 网站维护具体做啥seo是什么职位