You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit introduces a Redis caching layer to the DBAL adapter to improve performance and reduce database load.
Features:
- Policies loaded via `loadPolicy` and `loadFilteredPolicy` are now cached in Redis.
- Configurable Redis connection parameters (host, port, password, database, TTL, prefix).
- Automatic cache invalidation for the main policy cache (`all_policies`) when policies are modified.
- A `preheatCache()` method to proactively load all policies into Redis.
- The adapter remains fully functional if Redis is not configured.
Includes:
- Updates to `Adapter.php` to integrate caching logic.
- Addition of `predis/predis` as a dependency.
- Unit tests for the caching functionality in `AdapterWithRedisTest.php`.
- Documentation in `README.md` for the new feature.
Known limitations:
- Cache invalidation for `loadFilteredPolicy` currently only clears the global `all_policies` key, not specific filtered policy keys, to avoid using `KEYS` in production with Predis.
To improve performance and reduce database load, the adapter supports caching policy data using [Redis](https://redis.io/). When enabled, Casbin policies will be fetched from Redis if available, falling back to the database if the cache is empty.
68
+
69
+
#### Configuration
70
+
71
+
To enable Redis caching, pass a Redis configuration array as the second argument to the `Adapter::newAdapter()` method or the `Adapter` constructor.
72
+
73
+
Available Redis configuration options:
74
+
75
+
*`host` (string): Hostname or IP address of the Redis server. Default: `'127.0.0.1'`.
76
+
*`port` (int): Port number of the Redis server. Default: `6379`.
77
+
*`password` (string, nullable): Password for Redis authentication. Default: `null`.
$e = new Enforcer('path/to/model.conf', $adapter);
114
+
115
+
// ... rest of your Casbin usage
116
+
```
117
+
118
+
#### Cache Preheating
119
+
120
+
The adapter provides a `preheatCache()` method to proactively load all policies from the database and store them in the Redis cache. This can be useful during application startup or as part of a scheduled task to ensure the cache is warm, reducing latency on initial policy checks.
// Cache preheating failed (e.g., Redis not available or DB error)
130
+
echo "Casbin policy cache preheating failed.\n";
131
+
}
132
+
```
133
+
134
+
#### Cache Invalidation
135
+
136
+
The cache is designed to be automatically invalidated when policy-modifying methods are called on the adapter (e.g., `addPolicy()`, `removePolicy()`, `savePolicy()`, etc.). Currently, this primarily clears the cache key for all policies (`{$prefix}all_policies`).
137
+
138
+
**Important Note:** The automatic invalidation for *filtered policies* (policies loaded via `loadFilteredPolicy()`) is limited. Due to the way `predis/predis` client works and to avoid using performance-detrimental commands like `KEYS *` in production environments, the adapter does not automatically delete cache entries for specific filters by pattern. If you rely heavily on `loadFilteredPolicy` and make frequent policy changes, consider a lower TTL for your Redis cache or implement a more sophisticated cache invalidation strategy for filtered results outside of this adapter if needed. The main `{$prefix}all_policies` cache is cleared on any policy change, which means subsequent calls to `loadPolicy()` will refresh from the database and update this general cache.
0 commit comments