Skip to content

feat: add redis-caching #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2025
Merged

Conversation

alonexy
Copy link
Contributor

@alonexy alonexy commented May 30, 2025

feat: Add Redis caching layer for policies
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.

@leeqvip
Copy link
Member

leeqvip commented May 31, 2025

@alonexy Please fix test error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alonexy Don't change all the lines. I can't see which line was modified or added.

@Dobmod
Copy link
Member

Dobmod commented Jun 10, 2025

I'm wondering if this PR is really necessary, since CachedEnforcer already supports Redis caching. Would adding caching to the dbal-adapter be somewhat redundant in this case?🤔

@leeqvip leeqvip merged commit b0a7d81 into php-casbin:master Jun 15, 2025
6 checks passed
@leeqvip
Copy link
Member

leeqvip commented Jun 15, 2025

🎉 This PR is included in version 3.1.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants