-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-manager.sh
More file actions
executable file
·179 lines (147 loc) · 5.4 KB
/
cache-manager.sh
File metadata and controls
executable file
·179 lines (147 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
# Cache Manager for Bitrix CDN Server
# Author: Chibilyaev Alexandr <info@aachibilyaev.com>
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${BLUE}🗄️ Bitrix CDN Cache Manager${NC}"
echo "================================"
# Redis cache management
redis_stats() {
echo -e "\n${BLUE}📊 Redis Cache Statistics${NC}"
echo "============================="
# Get Redis password from environment or use default
local redis_password="${REDIS_PASSWORD:-bitrix_cdn_secure_2024}"
# Redis info
local redis_info=$(docker exec cdn-redis redis-cli -a "$redis_password" info memory | grep used_memory_human)
echo "Redis memory usage: $redis_info"
# Redis keys count
local keys=$(docker exec cdn-redis redis-cli -a "$redis_password" dbsize)
echo "Redis keys count: $keys"
# Redis hit rate
local hits=$(docker exec cdn-redis redis-cli -a "$redis_password" info stats | grep keyspace_hits | cut -d: -f2 | tr -d '\r')
local misses=$(docker exec cdn-redis redis-cli -a "$redis_password" info stats | grep keyspace_misses | cut -d: -f2 | tr -d '\r')
if [ "$hits" -gt 0 ] || [ "$misses" -gt 0 ]; then
local hit_rate=$(echo "scale=2; $hits * 100 / ($hits + $misses)" | bc -l 2>/dev/null || echo "0")
echo "Redis hit rate: ${hit_rate}%"
fi
# Redis connected clients
local clients=$(docker exec cdn-redis redis-cli -a "$redis_password" info clients | grep connected_clients | cut -d: -f2 | tr -d '\r')
echo "Connected clients: $clients"
# Redis operations per second
local ops=$(docker exec cdn-redis redis-cli -a "$redis_password" info stats | grep instantaneous_ops_per_sec | cut -d: -f2 | tr -d '\r')
echo "Operations per second: $ops"
}
# Memcached removed - Redis is sufficient for caching
# Nginx cache management
nginx_cache_stats() {
echo -e "\n${BLUE}📊 Nginx Cache Statistics${NC}"
echo "=============================="
# Nginx cache directory size
local cache_size=$(docker exec cdn-nginx du -sh /tmp/nginx_cache 2>/dev/null | awk '{print $1}' || echo "0")
echo "Nginx cache size: $cache_size"
# Nginx cache files count
local cache_files=$(docker exec cdn-nginx find /tmp/nginx_cache -type f 2>/dev/null | wc -l || echo "0")
echo "Nginx cache files: $cache_files"
# Nginx memory usage
local nginx_mem=$(docker stats cdn-nginx --no-stream --format "table {{.MemUsage}}" | tail -1)
echo "Nginx memory usage: $nginx_mem"
}
# Clear Redis cache
clear_redis() {
echo -e "\n${YELLOW}🗑️ Clearing Redis cache...${NC}"
# Get Redis password from environment or use default
local redis_password="${REDIS_PASSWORD:-bitrix_cdn_secure_2024}"
docker exec cdn-redis redis-cli -a "$redis_password" flushall
echo -e "${GREEN}✅ Redis cache cleared${NC}"
}
# Memcached removed - Redis is sufficient for caching
# Clear Nginx cache
clear_nginx() {
echo -e "\n${YELLOW}🗑️ Clearing Nginx cache...${NC}"
docker exec cdn-nginx rm -rf /tmp/nginx_cache/*
echo -e "${GREEN}✅ Nginx cache cleared${NC}"
}
# Clear all caches
clear_all() {
echo -e "\n${YELLOW}🗑️ Clearing all caches...${NC}"
clear_redis
# Memcached removed
clear_nginx
echo -e "${GREEN}✅ All caches cleared${NC}"
}
# Cache warming
warm_cache() {
echo -e "\n${BLUE}🔥 Warming up caches...${NC}"
# Warm up with common requests
local urls=(
"/health"
"/nginx_status"
"/upload/resize_cache/test.jpg"
"/upload/resize_cache/test.webp"
"/upload/resize_cache/test.avif"
)
for url in "${urls[@]}"; do
echo "Warming: $url"
curl -s "http://localhost$url" > /dev/null 2>&1 || true
done
echo -e "${GREEN}✅ Cache warming complete${NC}"
}
# Cache optimization
optimize_cache() {
echo -e "\n${BLUE}⚡ Optimizing caches...${NC}"
# Redis optimization
echo "Optimizing Redis..."
docker exec cdn-redis redis-cli config set maxmemory-policy allkeys-lru
docker exec cdn-redis redis-cli config set save ""
# Memcached removed - Redis is sufficient for caching
# Nginx optimization
echo "Optimizing Nginx..."
docker exec cdn-nginx nginx -s reload
echo -e "${GREEN}✅ Cache optimization complete${NC}"
}
# Main function
main() {
case "$1" in
redis-stats)
redis_stats
;;
nginx-cache)
nginx_cache_stats
;;
clear-redis)
clear_redis
;;
clear-nginx)
clear_nginx
;;
clear-all)
clear_all
;;
warm)
warm_cache
;;
optimize)
optimize_cache
;;
*)
echo "Usage: $0 {redis-stats|nginx-cache|clear-redis|clear-nginx|clear-all|warm|optimize}"
echo ""
echo "Commands:"
echo " redis-stats - Show Redis cache statistics"
echo " nginx-cache - Show Nginx cache statistics"
echo " clear-redis - Clear Redis cache"
echo " clear-nginx - Clear Nginx cache"
echo " clear-all - Clear all caches"
echo " warm - Warm up caches"
echo " optimize - Optimize cache settings"
exit 1
;;
esac
}
# Run
main "$@"