mybatis / mybatis-3

MyBatis SQL mapper framework for Java
http://mybatis.github.io/mybatis-3/
Apache License 2.0
19.76k stars 12.85k forks source link

Custom implementation of CacheKey? #1569

Open binakot opened 5 years ago

binakot commented 5 years ago

Good day :octocat:

I'm working on smart cache for MyBatis with external Redis storage based on my domain.

How can I change implementation of default CacheKey? I want to use combination of path to method in mapper and list of function arguments and smth else.

It looks like it's hard-coded in BaseExecutor. Can I inherit and change implementation for createCacheKey only? Without reflection of forking this project?

harawata commented 5 years ago

Hello, @binakot !

This may be related to #1541 . Could you elaborate on 'smth else' ? If I understand correctly, 'path to method in mapper' and 'list of function arguments' are already added to CacheKey and I would like to know what kind of extra data you want to add.

The reporter of #1541 , for example, wanted to add 'client id' retrieved from ThreadLocal.

binakot commented 5 years ago

Hello, @binakot !

This may be related to #1541 . Could you elaborate on 'smth else' ? If I understand correctly, 'path to method in mapper' and 'list of function arguments' are already added to CacheKey and I would like to know what kind of extra data you want to add.

The reporter of #1541 , for example, wanted to add 'client id' retrieved from ThreadLocal.

In fact I want to remove everything from cache key result string except method name and parameters list. I have an idea to use mybatis cache in redis as distributed cache between few applications.

If I load some data from one application, it's going to cache. After the second application will get data from cache, because both applications will have same cache key in common Redis instance.

Thanks for the related issue, it looks close to this one. But it's still opened and there are no any suggestions 😢

harawata commented 5 years ago

1541 is a feature request and we are still investigating its possibility.

I want to remove everything from cache key result string except method name and parameters list.

There is nothing conclusive yet, but it seems unlikely that we allow users to create a custom CacheKey implementation like that because it will prevent MyBatis from functioning properly. Have you considered managing your cache outside of MyBatis?

binakot commented 5 years ago

1541 is a feature request and we are still investigating its possibility.

I want to remove everything from cache key result string except method name and parameters list.

There is nothing conclusive yet, but it seems unlikely that we allow users to create a custom CacheKey implementation like that because it will prevent MyBatis from functioning properly. Have you considered managing your cache outside of MyBatis?

At this moment I'm looking for possibility to manage my cache inside MyBatis, because we are really hard depending on this layer in our applications. You can provide default implementation, but let to developers do custom implementations. And if it will break functionality of MyBatis, it is the problem of developers, isn't it? 😄That's why now we have to hack the MyBatis with reflection...

binakot commented 5 years ago

Default implementation of CacheKey gives that:

image

Our custom implementation gives that:

image