This is the source of the key class Rediscache in Mybatis/redis-cache
/*** Copyright The original author or authors. * Licensed under the Apache License, Version 2.0 (the "Licen Se "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is Dist Ributed on an ' as is ' BASIS, * without warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageOrg.mybatis.caches.redis;ImportJava.util.Map;ImportJava.util.concurrent.locks.ReadWriteLock;ImportOrg.apache.ibatis.cache.Cache;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;/*** Cache Adapter for Redis. * *@authorEduardo Macarron*/ Public Final classRediscacheImplementsCache {Private FinalReadwritelock Readwritelock =NewDummyreadwritelock (); PrivateString ID; Private StaticJedispool Pool; PublicRediscache (FinalString ID) { if(id = =NULL) { Throw NewIllegalArgumentException ("Cache instances require an ID"); } This. ID =ID; Redisconfig Redisconfig=redisconfigurationbuilder.getinstance (). Parseconfiguration (); Pool=NewJedispool (Redisconfig, Redisconfig.gethost (), Redisconfig.getport (), Redisconfig.getconnectiontimeout (), R Edisconfig.getsotimeout (), Redisconfig.getpassword (), Redisconfig.getdatabase (), Redisconfig.getclientname ()); } PrivateObject Execute (rediscallback callback) {Jedis Jedis=Pool.getresource (); Try { returnCallback.dowithredis (Jedis); } finally{jedis.close (); }} @Override PublicString getId () {return This. ID; } @Override Public intGetSize () {return(Integer) Execute (NewRediscallback () {@Override PublicObject Dowithredis (Jedis jedis) {Map<byte[],byte[]> result =Jedis.hgetall (Id.tostring (). GetBytes ()); returnresult.size (); } }); } @Override Public voidPutObject (FinalObject Key,FinalObject value) {Execute (NewRediscallback () {@Override PublicObject Dowithredis (Jedis Jedis) {Jedis.hset (Id.tostring (). GetBytes (), key.tostring (). GetBytes (), Serializeuti L.serialize (value)); return NULL; } }); } @Override PublicObject GetObject (FinalObject Key) { returnExecuteNewRediscallback () {@Override PublicObject Dowithredis (Jedis Jedis) {returnserializeutil.unserialize (Jedis.hget (id.tostring () getBytes (), key.tostring (). GetBytes ()); } }); } @Override PublicObject Removeobject (FinalObject Key) { returnExecuteNewRediscallback () {@Override PublicObject Dowithredis (Jedis Jedis) {returnJedis.hdel (id.tostring (), key.tostring ()); } }); } @Override Public voidClear () {Execute (NewRediscallback () {@Override PublicObject Dowithredis (Jedis Jedis) {Jedis.del (id.tostring ()); return NULL; } }); } @Override PublicReadwritelock Getreadwritelock () {returnReadwritelock; } @Override PublicString toString () {return"Redis {" + id + "}"; }}
You can see that a lot of the callback methods are used to manipulate Redis, but the call itself is synchronous, and the only benefit that can be thought of is that it saves close () after each call-not knowing if there are any other advantages.
The callback in Redis-cache