feat: add pre-write interceptors for external caches#1006
feat: add pre-write interceptors for external caches#1006youjie23 wants to merge 7 commits intoalibaba:masterfrom
Conversation
|
This change modifies too many files just to implement one feature, and I think this requirement can be achieved using |
Thanks for the review. I agree, the PR is relatively large, but I do not think They are method-level controls for From a global cache governance/observability perspective, putting the same generic logic on every annotation would be a maintenance burden. For use cases like auditing, metrics, write validation, or big-key detection, duplicating expressions across many methods is hard to keep consistent and easy to miss. There is also a data gap: Therefore, I think If you think the current PR is too large to review comfortably, I can split it into smaller ones to make the review easier. |
|
JetCache itself is highly customizable, allowing users to handle many things on their own. For example, to implement this feature, users can simply customize the Cache creation process and wrap it with a ProxyCache before returning the Cache instance. There’s no need for such a complicated approach. This change is far too extensive, so I cannot accept it. |
Thanks for your feedback, understood. I agree that JetCache is customizable and that a user-side solution can be built by customizing cache creation and wrapping the returned Cache with a ProxyCache. If the project preference is to keep this kind of capability out of the core, I respect that. The main reason I proposed a built-in hook was not that this is impossible on the user side, but that the key part of the change lies inside the concrete remote cache implementations. That is where the extension point would ideally exist to enable this capability both reliably and efficiently. Most of the additional changes in this PR are there to make that hook usable through configuration, annotations, QuickConfig, starter integration, documentation, and tests. A ProxyCache-based solution operates at the Cache API layer rather than inside the concrete remote write path. Because of that, it does not naturally have access to some final write metadata produced by the backend implementation, especially post-encoding data such as the final encoded key/value size. This point matters for scenarios like big-key detection and interception. In a ProxyCache approach, obtaining the encoded size would typically require re-encoding in user code, which adds extra serialization cost and may also deviate from the actual encoder/configuration used by the remote cache implementation. Therefore, I still believe there is a technical difference between a user-side ProxyCache wrapper and a built-in remote-write hook. However, I understand your concern about the size and invasiveness of this PR. May I kindly ask for your guidance on how you'd prefer to proceed with this PR? For example:
I'm happy to adapt my next steps based on your feedback. Thank you for considering it. |
Background
JetCache currently lacks a unified extension point before remote cache writes.
When users want to add logging, auditing, metrics, big-key detection, or write rejection before cache writes, they usually have to rely on one of these approaches:
Each of these approaches has clear limitations:
@Cached,@CreateCache, andCacheManager.getOrCreateCache(QuickConfig), and it cannot observe the final encoded payload size.Because of that, JetCache needs a unified pre-write interception capability at the external cache abstraction layer.
What this PR changes
This PR introduces
ExternalCacheWriteInterceptorand executes it before external cache write attempts.The change includes:
PUT,PUT_ALL, andPUT_IF_ABSENT.@Cached,@CreateCache,QuickConfig, and starter-level global configuration.Why this is valuable
CacheResultinstead of being thrown directly from the base write methods, which keeps the existing JetCache API styleconsistent.