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

服务端Web资源缓存

1.前言

虽然客户端缓存效果很好,但它有一个核心问题:要在本地提供资源,必须先将其存储在缓存中。因此,每个客户端都需要其缓存的资源。如果请求的资源需要大量计算,则无法扩展。服务器端缓存背后的理念是计算一次资源,然后从缓存中将其提供给所有客户端。
在这里插入图片描述
专用的服务器端资源缓存解决方案: Memcached,Varnish,Squid、Redis、Hazelcast。

2.演示

使用 Apache APISIX 来演示服务器端缓存。APISIX 依赖于代理缓存插件进行缓存。不幸的是,目前 APISIX 不与任何第三方缓存解决方案集成。它提供两种选项:基于内存和基于磁盘。

一般来说,前者速度更快,但内存昂贵,而后者速度较慢,但​​磁盘存储便宜。然而,在 OpenResty 中,由于 LuaJIT 处理内存的方式,磁盘选项可能更快。您可能应该从磁盘开始,如果速度不够快,请挂载/dev/shm。

2.1.创建新路由

apisix.yaml配置:

routes:- uri: /cacheupstream_id: 1plugins:proxy-rewrite:regex_uri: ["/cache(.*)", "/$1"]proxy-cache: ~

请注意,默认缓存键是主机和请求 URI,其中包括查询参数。
当然也可以通过修改路由配置绕过缓存,配置如下:

routes:- uri: /cache*upstream_id: 1proxy-cache:cache_bypass: ["$arg_bypass"]   
curl -v localhost:9080/cache?bypass=please

2.2.默认proxy-cache配置

使用默认的基于磁盘的配置。
config-default.yaml配置:

  proxy_cache:                      # Proxy Caching configurationcache_ttl: 10s                  # The default caching time in disk if the upstream does not specify the cache timezones:                          # The parameters of a cache- name: disk_cache_one        # The name of the cache, administrator can specify# which cache to use by name in the admin api (disk|memory)memory_size: 50m            # The size of shared memory, it's used to store the cache index for# disk strategy, store cache content for memory strategy (disk|memory)disk_size: 1G               # The size of disk, it's used to store the cache data (disk)disk_path: /tmp/disk_cache_one  # The path to store the cache data (disk)cache_levels: 1:2           # The hierarchy levels of a cache (disk)- name: memory_cachememory_size: 50m

启动对应的应用,即可进行后续的测试工作。

2.3.测试

2.3.1.测试命令

curl -v localhost:9080/cache
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 147
< Connection: keep-alive
< Date: Thu, 16 May 2024 06:35:00 GMT
< Last-Modified: Thu, 16 May 2024 07:41:00 GMT
< ETag: "637e271f-93"
< Server: APISIX/3.0.0
< Apisix-Cache-Status: MISS                      
< Accept-Ranges: bytes

2.3.2.MISS

.
.
.
Apisix-Cache-Status: MISS 
.
.
.

2.3.3.HIT

如果我们curl再次在默认缓存过期时间(300 秒)之前,响应来自缓存

.
.
.
Apisix-Cache-Status: HIT
.
.
.

2.3.4.EXPIRED

.
.
.
Apisix-Cache-Status: EXPIRED
.
.
.

2.3.5.清除整个缓存

请注意,我们可以使用自定义 PURGE HTTP 方法显式清除整个缓存:

curl localhost:9080/cache -X PURGE
http://www.lryc.cn/news/350575.html

相关文章:

  • STM32-09-IWDG
  • Android手动下载Gradle的使用方法
  • 2024彩虹医械维修培训邀请
  • 车辆超龄无法注册滴滴司机怎么办理账号
  • MATLAB车辆动力学建模 ——《控制系统现代开发技术》
  • 复杂json解析(其中有一个key的value是json格式的字符串)
  • 线程池的一些问题
  • 企业或者个体户为什么会经营异常?
  • ROS从入门到精通4-3:制作Docker镜像文件Dockerfile
  • 【CV】计算机视觉是什么?
  • 如何在Vue中实现鼠标悬浮展示与隐藏弹窗的功能
  • 03 Linux编程-进程
  • Hbuild-X运行ios基座app
  • Node.js基础:从入门到实战
  • 考研408笔记总结~
  • 使用在线工具等方式下载推特视频
  • 性能优化:几方面考虑
  • 学习大数据:论学习Spark的重要性
  • 学习java第七十一天
  • Altium Designer PCB快捷键设置
  • 玩转Matlab-Simscape(初级)- 08 - 基于Solidworks、Matlab Simulink、COMSOL的协同仿真(案例实战)
  • vue嵌套路由
  • 视频降噪算法 hqdn3d 原理分析
  • Ansys Mechanical|屈曲分析技术
  • 【大模型微调】一文掌握7种大模型微调的方法
  • MySQL表突然卡死,删、查操作加载不停解决办法
  • Rust 标准库的结构及其模块路径
  • 003_PyCharm的安装与使用
  • 事件传递机制
  • DE2-115串口通信