srcache_nginx+redis构建缓存系统

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:

   本文将使用redis来作为缓存载体。nginx的srcache_nginx模块指令参数解释参见《memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存》。

1. nginx模块

--add-module=../modules/ngx_devel_kit-0.2.18

--add-module=../modules/set-misc-nginx-module-0.22rc8

--add-module=../modules/srcache-nginx-module-0.22

--add-module=../modules/redis-nginx-module-0.3.6

--add-module=../modules/redis2-nginx-module-0.10


2. redis安装配置

安装步骤参见:http://www.ttlsa.com/html/1646.html
配置参数解释参见:http://www.ttlsa.com/html/1226.html
配置实例:
# vim redis.conf

daemonize yes

pidfile /var/run/redis-6379.pid

port 6379

bind 127.0.0.1

timeout 0

tcp-keepalive 0

loglevel notice

logfile stdout

databases 16

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

maxmemory 8096mb

maxmemory-policy volatile-ttl

appendonly no

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

由于只把redis当做缓存使用,因此没有启用持久化。

3. nginx配置

# vim nginx.conf

http

{

        include       mime.types;

        default_type  application/octet-stream;

 

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                                        '"$status" $body_bytes_sent "$http_referer" '

                                        '"$http_user_agent" "$http_x_forwarded_for" '

                                        '"$gzip_ratio" $request_time $bytes_sent $request_length';

 

        log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" '

                                '"$status" $body_bytes_sent $request_time $bytes_sent $request_length '

                                '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';

 

        set_real_ip_from 10.0.0.0/8;

        real_ip_header X-Forwarded-For;

 

        include          vhosts/test.ttlsa.com.conf;

}

# vim vhosts/test.ttlsa.com.conf

upstream redis {

        server 127.0.0.1:6379;

        keepalive 512;

}

 

server

       {

        listen       80;

        server_name  test.ttlsa.com;

        index index.html index.htm index.php;

        root  /data/test.ttlsa.com/webroot;

 

        location ~ .*\.php {

                srcache_store_private on;

                srcache_methods GET;

                srcache_response_cache_control off;

 

                if ($uri ~ /ttlsa.com/pp.php$){

                        set $key $request_uri;

                        set_escape_uri $escaped_key $key;

                        srcache_fetch GET /redis $key;

                        srcache_default_expire 172800;

                        srcache_store PUT /redis2 key=$escaped_key&exptime=$srcache_expire;

 

                        #add_header X-Cached-From $srcache_fetch_status;

                        #set_md5 $md5key $key;

                        #add_header X-md5-key $md5key;

                        #add_header X-Cached-Store $srcache_store_status;

                        #add_header X-Key $key;

                        #add_header X-Query_String $query_string;

                        #add_header X-expire $srcache_expire;

 

access_log /data/httplogs/test.ttlsa.com-photo-access.log srcache_log;

                }

 

                include fastcgi_params;

                fastcgi_pass  127.0.0.1:9000;

                fastcgi_index index.php;

                fastcgi_connect_timeout 60;

                fastcgi_send_timeout 180;

                fastcgi_read_timeout 180;

                fastcgi_buffer_size 128k;

                fastcgi_buffers 4 256k;

                fastcgi_busy_buffers_size 256k;

                fastcgi_temp_file_write_size 256k;

                fastcgi_intercept_errors on;

                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

                fastcgi_split_path_info ^(.+\.php)(.*)$;

                fastcgi_param PATH_INFO $fastcgi_path_info;

         }

 

        location = /redis {

                internal;

                set_md5 $redis_key $args;

                redis_pass redis;

        }

 

        location = /redis2 {

                internal;

 

                set_unescape_uri $exptime $arg_exptime;

                set_unescape_uri $key $arg_key;

                set_md5 $key;

 

                redis2_query set $key $echo_request_body;

                redis2_query expire $key $exptime;

                redis2_pass redis;

        }

 

        error_log  /data/httplogs/test.ttlsa.com-error.log;

        access_log  /data/httplogs/test.ttlsa.com-aceess.log main;

 

}


4. 测试

没有做缓存状态:

memc-nginx

有做缓存状态:

memc-nginx

5. 响应头状态

第一次请求:


memc-nginx

再次请求:

memc-nginx


6. 查看redis是否缓存以及过期时间


memc-nginx



      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1751607,如需转载请自行联系原作者




相关文章
|
5月前
|
缓存 NoSQL 关系型数据库
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
|
27天前
|
存储 缓存 NoSQL
Redis专题-实战篇二-商户查询缓存
本文介绍了缓存的基本概念、应用场景及实现方式,涵盖Redis缓存设计、缓存更新策略、缓存穿透问题及其解决方案。重点讲解了缓存空对象与布隆过滤器的使用,并通过代码示例演示了商铺查询的缓存优化实践。
119 1
Redis专题-实战篇二-商户查询缓存
|
12天前
|
缓存 负载均衡 监控
135_负载均衡:Redis缓存 - 提高缓存命中率的配置与最佳实践
在现代大型语言模型(LLM)部署架构中,缓存系统扮演着至关重要的角色。随着LLM应用规模的不断扩大和用户需求的持续增长,如何构建高效、可靠的缓存架构成为系统性能优化的核心挑战。Redis作为业界领先的内存数据库,因其高性能、丰富的数据结构和灵活的配置选项,已成为LLM部署中首选的缓存解决方案。
|
13天前
|
缓存 运维 监控
Redis 7.0 高性能缓存架构设计与优化
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Redis 7.0高性能缓存架构,探索函数化编程、多层缓存、集群优化与分片消息系统,用代码在二进制星河中谱写极客诗篇。
|
5月前
|
缓存 NoSQL Java
Redis+Caffeine构建高性能二级缓存
大家好,我是摘星。今天为大家带来的是Redis+Caffeine构建高性能二级缓存,废话不多说直接开始~
777 0
|
2月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
867 10
|
28天前
|
缓存 NoSQL 关系型数据库
Redis缓存和分布式锁
Redis 是一种高性能的键值存储系统,广泛用于缓存、消息队列和内存数据库。其典型应用包括缓解关系型数据库压力,通过缓存热点数据提高查询效率,支持高并发访问。此外,Redis 还可用于实现分布式锁,解决分布式系统中的资源竞争问题。文章还探讨了缓存的更新策略、缓存穿透与雪崩的解决方案,以及 Redlock 算法等关键技术。
|
3月前
|
存储 缓存 监控
手动清除Ubuntu系统中的内存缓存的步骤
此外,只有系统管理员或具有适当权限的用户才能执行这些命令,因为这涉及到系统级的操作。普通用户尝试执行这些操作会因权限不足而失败。
555 22
|
2月前
|
缓存 监控 Ubuntu
Ubuntu操作系统下清除系统缓存与无用文件的方法
通过上述步骤断行综合性地对Ubuntu进行优化与整洁可显著改善其性能表现及响应速度。然而,请注意在执行某些操作前确保充分了解其潜在影响;例如,在移除旧内核之前确认新内核稳定运行无问题;而对于关键配置更改则需确保备份好相关设置以便恢复原状态。
344 0
|
5月前
|
应用服务中间件 Linux 网络安全
技术指南:如何把docsify项目部署到基于CentOS系统的Nginx中。
总结 与其他部署方法相比,将docsify项目部署到基于CentOS系统的Nginx中比较简单。以上步骤应当帮助你在不花费太多时间的情况下,将你的项目顺利部署到Nginx中。迈出第一步,开始部署你的docsify项目吧!
221 14