Skip to content

Commit 2517385

Browse files
authored
✨ Add Redis object cache support (#1589)
1 parent 608621e commit 2517385

File tree

15 files changed

+379
-7
lines changed

15 files changed

+379
-7
lines changed

.github/files/vault.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ vault_wordpress_sites:
3434
secure_auth_salt: "generateme"
3535
logged_in_salt: "generateme"
3636
nonce_salt: "generateme"
37+
redis.example.com:
38+
env:
39+
db_password: example_dbpassword
40+
# Generate your keys here: https://roots.io/salts.html
41+
auth_key: "generateme"
42+
secure_auth_key: "generateme"
43+
logged_in_key: "generateme"
44+
nonce_key: "generateme"
45+
auth_salt: "generateme"
46+
secure_auth_salt: "generateme"
47+
logged_in_salt: "generateme"
48+
nonce_salt: "generateme"

.github/files/wordpress_sites.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,22 @@ wordpress_sites:
2929
provider: letsencrypt
3030
cache:
3131
enabled: false
32+
redis.example.com:
33+
site_hosts:
34+
- canonical: redis.example.com
35+
redirects:
36+
- www.redis.example.com
37+
local_path: ../site
38+
repo: [email protected]:roots/bedrock.git
39+
branch: master
40+
multisite:
41+
enabled: false
42+
ssl:
43+
enabled: false
44+
provider: letsencrypt
45+
cache:
46+
enabled: true
47+
object_cache:
48+
enabled: true
49+
provider: redis
50+
database: 0

.github/workflows/integration.yml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
run: trellis new --name example.com --host www.example.com --trellis-version ${{ github.sha }} ./example.com
4444
- name: Update configs
4545
run: |
46-
sudo echo "127.0.0.1 www.example.com example.com www.example-https.com example-https.com" | sudo tee -a /etc/hosts
46+
sudo echo "127.0.0.1 www.example.com example.com www.example-https.com example-https.com www.redis.example.com redis.example.com" | sudo tee -a /etc/hosts
4747
cp ../../.github/files/inventory hosts/production
4848
cp ../../.github/files/wordpress_sites.yml group_vars/production/wordpress_sites.yml
4949
cp ../../.github/files/vault.yml group_vars/production/vault.yml
@@ -74,3 +74,38 @@ jobs:
7474
working-directory: /srv/www/example-https.com/current
7575
- name: Verify install
7676
run: curl -s https://example-https.com | grep "<title>Example HTTPS"
77+
- name: Deploy Redis site
78+
run: trellis deploy --extra-vars "web_user=runner project_git_repo=https://github.com/roots/bedrock.git" production redis.example.com
79+
working-directory: example.com
80+
- name: Install WordPress on Redis site
81+
run: |
82+
wp core install --url="http://redis.example.com" --title="Redis Example" --admin_user="admin" --admin_password="password" --admin_email="[email protected]"
83+
working-directory: /srv/www/redis.example.com/current
84+
- name: Verify Redis service
85+
run: |
86+
systemctl is-active redis-server
87+
redis-cli ping
88+
- name: Verify PHP Redis extension
89+
run: php -m | grep redis
90+
- name: Test Redis basic functionality
91+
run: |
92+
redis-cli SET test_key "test_value"
93+
redis-cli GET test_key | grep "test_value"
94+
- name: Verify Redis environment variables
95+
run: |
96+
grep "WP_REDIS_HOST" /srv/www/redis.example.com/current/.env
97+
grep "WP_REDIS_PORT" /srv/www/redis.example.com/current/.env
98+
grep "WP_REDIS_DATABASE" /srv/www/redis.example.com/current/.env
99+
- name: Verify Redis site install
100+
run: curl -s http://redis.example.com | grep "<title>Redis Example"
101+
- name: Verify FastCGI cache on example.com
102+
run: |
103+
# First request - check headers
104+
echo "=== First request ==="
105+
curl -I http://example.com | grep -i "fastcgi-cache"
106+
# Second request - check headers
107+
echo "=== Second request ==="
108+
curl -I http://example.com | grep -i "fastcgi-cache"
109+
# Third request - check headers (should be HIT by now)
110+
echo "=== Third request ==="
111+
curl -I http://example.com | grep -iE "fastcgi-cache: (MISS|HIT|STALE)"

dev.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
- { role: mailpit, tags: [mailpit, mailhog, mail] }
1515
- { role: php, tags: [php] }
1616
- { role: xdebug, tags: [php, xdebug] }
17-
- { role: memcached, tags: [memcached] }
17+
- { role: memcached, tags: [memcached], when: sites_using_memcached | count }
18+
- { role: redis, tags: [redis], when: sites_using_redis | count }
1819
- { role: nginx, tags: [nginx] }
1920
- { role: logrotate, tags: [logrotate] }
2021
- { role: composer, tags: [composer] }

group_vars/all/helpers.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ wordpress_env_defaults:
1010
domain_current_site: "{{ site_hosts_canonical | first }}"
1111
wp_debug_log: "{{ www_root }}/{{ item.key }}/logs/debug.log"
1212

13-
site_env: "{{ wordpress_env_defaults | combine(vault_wordpress_env_defaults | default({}), item.value.env | default({}), vault_wordpress_sites[item.key].env) }}"
13+
# Redis cache environment variables (when enabled)
14+
redis_cache_env:
15+
wp_redis_host: "{{ item.value.object_cache.host | default('127.0.0.1') }}"
16+
wp_redis_port: "{{ item.value.object_cache.port | default(6379) }}"
17+
wp_redis_database: "{{ item.value.object_cache.database | default(0) }}"
18+
wp_redis_prefix: "{{ item.value.object_cache.prefix | default(item.key | underscore + '_') }}"
19+
wp_cache_key_salt: "{{ item.key }}_{{ env }}"
20+
21+
# Memcached cache environment variables (when enabled)
22+
memcached_cache_env:
23+
wp_memcached_host: "{{ item.value.object_cache.host | default('127.0.0.1') }}"
24+
wp_memcached_port: "{{ item.value.object_cache.port | default(11211) }}"
25+
wp_memcached_prefix: "{{ item.value.object_cache.prefix | default(item.key | underscore + '_') }}"
26+
wp_cache_key_salt: "{{ item.key }}_{{ env }}"
27+
28+
site_env: "{{ wordpress_env_defaults | combine(vault_wordpress_env_defaults | default({}), object_cache_enabled_redis | ternary(redis_cache_env, {}), object_cache_enabled_memcached | ternary(memcached_cache_env, {}), item.value.env | default({}), vault_wordpress_sites[item.key].env) }}"
29+
30+
object_cache_enabled_redis: "{{ item.value.object_cache.enabled | default(false) and item.value.object_cache.provider | default('') == 'redis' }}"
31+
object_cache_enabled_memcached: "{{ item.value.object_cache.enabled | default(false) and item.value.object_cache.provider | default('') == 'memcached' }}"
32+
33+
# Sites using Redis or Memcached object cache
34+
sites_using_redis: "[{% for name, site in wordpress_sites.items() | list if site.object_cache.enabled | default(false) and site.object_cache.provider | default('') == 'redis' %}'{{ name }}',{% endfor %}]"
35+
sites_using_memcached: "[{% for name, site in wordpress_sites.items() | list if site.object_cache.enabled | default(false) and site.object_cache.provider | default('') == 'memcached' %}'{{ name }}',{% endfor %}]"
1436
site_hosts_canonical: "{{ item.value.site_hosts | map(attribute='canonical') | list }}"
1537
site_hosts_redirects: "{{ item.value.site_hosts | selectattr('redirects', 'defined') | sum(attribute='redirects', start=[]) | list }}"
1638
site_hosts: "{{ site_hosts_canonical | union(site_hosts_redirects) }}"

group_vars/development/wordpress_sites.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ wordpress_sites:
1919
enabled: false
2020
xmlrpc:
2121
enabled: false
22+

roles/deploy/vars/main.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,23 @@ wordpress_env_defaults:
1212
wp_debug_log: "{{ project_root }}/logs/debug.log"
1313
wp_post_revisions: true
1414

15-
site_env: "{{ wordpress_env_defaults | combine(vault_wordpress_env_defaults | default({}), project.env | default({}), vault_wordpress_sites[site].env) }}"
15+
# Redis cache environment variables for deployment
16+
deploy_redis_cache_env:
17+
wp_redis_host: "{{ project.object_cache.host | default('127.0.0.1') }}"
18+
wp_redis_port: "{{ project.object_cache.port | default(6379) }}"
19+
wp_redis_database: "{{ project.object_cache.database | default(0) }}"
20+
wp_redis_prefix: "{{ project.object_cache.prefix | default(site | underscore + '_') }}"
21+
wp_cache_key_salt: "{{ site }}_{{ env }}"
22+
23+
# Memcached cache environment variables for deployment
24+
deploy_memcached_cache_env:
25+
wp_memcached_host: "{{ project.object_cache.host | default('127.0.0.1') }}"
26+
wp_memcached_port: "{{ project.object_cache.port | default(11211) }}"
27+
wp_memcached_prefix: "{{ project.object_cache.prefix | default(site | underscore + '_') }}"
28+
wp_cache_key_salt: "{{ site }}_{{ env }}"
29+
30+
site_env: "{{ wordpress_env_defaults | combine(vault_wordpress_env_defaults | default({}), deploy_object_cache_enabled_redis | ternary(deploy_redis_cache_env, {}), deploy_object_cache_enabled_memcached | ternary(deploy_memcached_cache_env, {}), project.env | default({}), vault_wordpress_sites[site].env) }}"
31+
32+
# Helper variables for deploy object cache conditions
33+
deploy_object_cache_enabled_redis: "{{ project.object_cache.enabled | default(false) and project.object_cache.provider | default('') == 'redis' }}"
34+
deploy_object_cache_enabled_memcached: "{{ project.object_cache.enabled | default(false) and project.object_cache.provider | default('') == 'memcached' }}"

roles/php/vars/version-specific-defaults.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ php_extensions_default:
2626
php_memcached_packages:
2727
"php{{ php_version }}-memcached": "{{ apt_package_state }}"
2828

29+
php_redis_packages:
30+
"php{{ php_version }}-redis": "{{ apt_package_state }}"
31+
2932
php_xdebug_package: "php{{ php_version }}-xdebug"

roles/redis/defaults/main.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
# Basic Redis configuration
3+
redis_daemonize: "yes"
4+
redis_pidfile: /run/redis/redis-server.pid
5+
redis_port: 6379
6+
redis_bind_interface: 127.0.0.1
7+
redis_unixsocket: /var/run/redis/redis.sock
8+
redis_timeout: 0
9+
redis_tcp_keepalive: 0
10+
11+
# Logging
12+
redis_loglevel: notice
13+
redis_logfile: /var/log/redis/redis-server.log
14+
15+
# Database configuration
16+
redis_databases: 16
17+
18+
# Persistence - RDB snapshots
19+
redis_save:
20+
- "900 1"
21+
- "300 10"
22+
- "60 10000"
23+
24+
redis_stop_writes_on_bgsave_error: "yes"
25+
redis_rdbcompression: "yes"
26+
redis_rdbchecksum: "yes"
27+
redis_dbfilename: dump.rdb
28+
redis_dir: /var/lib/redis
29+
30+
# Memory management
31+
redis_maxclients: 10000
32+
redis_maxmemory: 256mb
33+
redis_maxmemory_policy: allkeys-lru
34+
redis_maxmemory_samples: 5
35+
36+
# Append Only File (AOF) persistence
37+
redis_appendonly: "no"
38+
redis_appendfilename: appendonly.aof
39+
redis_appendfsync: everysec
40+
redis_no_appendfsync_on_rewrite: "no"
41+
redis_auto_aof_rewrite_percentage: 100
42+
redis_auto_aof_rewrite_min_size: 64mb
43+
redis_aof_load_truncated: "yes"
44+
45+
# Advanced configuration
46+
redis_lua_time_limit: 5000
47+
redis_slowlog_log_slower_than: 10000
48+
redis_slowlog_max_len: 128
49+
redis_latency_monitor_threshold: 0
50+
51+
# Hash table optimizations
52+
redis_hash_max_ziplist_entries: 512
53+
redis_hash_max_ziplist_value: 64
54+
redis_list_max_ziplist_entries: 512
55+
redis_list_max_ziplist_value: 64
56+
redis_set_max_intset_entries: 512
57+
redis_zset_max_ziplist_entries: 128
58+
redis_zset_max_ziplist_value: 64
59+
redis_hll_sparse_max_bytes: 3000
60+
redis_activerehashing: "yes"
61+
62+
# Client output buffer limits
63+
redis_client_output_buffer_limit_normal: "0 0 0"
64+
redis_client_output_buffer_limit_replica: "256mb 64mb 60"
65+
redis_client_output_buffer_limit_pubsub: "32mb 8mb 60"
66+
67+
# Misc
68+
redis_hz: 10
69+
redis_aof_rewrite_incremental_fsync: "yes"
70+
71+
# Security
72+
redis_requirepass: false
73+
redis_disabled_commands:
74+
- FLUSHDB
75+
- FLUSHALL
76+
- KEYS
77+
- CONFIG
78+
- SHUTDOWN
79+
80+
# Custom configuration
81+
redis_includes: []
82+
redis_extra_config: {}
83+
84+
redis_packages_default:
85+
redis-server: "{{ apt_package_state }}"
86+
87+
redis_packages_custom: {}
88+
redis_packages: "{{ redis_packages_default | combine(php_redis_packages, redis_packages_custom) }}"

roles/redis/handlers/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: restart redis
3+
service:
4+
name: redis-server
5+
state: restarted

0 commit comments

Comments
 (0)