The callback in Redis-cache

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.