diff --git a/obp-api/src/main/scala/code/api/cache/Caching.scala b/obp-api/src/main/scala/code/api/cache/Caching.scala index f5654b9a3..cb2b684ed 100644 --- a/obp-api/src/main/scala/code/api/cache/Caching.scala +++ b/obp-api/src/main/scala/code/api/cache/Caching.scala @@ -1,7 +1,7 @@ package code.api.cache import code.api.Constant._ -import code.api.cache.Redis.jedis +import code.api.cache.Redis.jedisConnection import code.api.util.APIUtil import code.util.Helper.MdcLoggable import com.softwaremill.macmemo.{Cache, MemoCacheBuilder, MemoizeParams} @@ -80,7 +80,7 @@ object Caching extends MdcLoggable { def getLocalisedResourceDocCache(key: String) = { val value = if(CREATE_LOCALISED_RESOURCE_DOC_JSON_TTL!=0 && Redis.isRedisAvailable()) - jedis.get(LOCALISED_RESOURCE_DOC_PREFIX + key) // if the key is not existing, jedis will return null + jedisConnection.get(LOCALISED_RESOURCE_DOC_PREFIX + key) // if the key is not existing, jedis will return null else null APIUtil.stringOrNone(value) @@ -88,12 +88,12 @@ object Caching extends MdcLoggable { def setLocalisedResourceDocCache(key:String, value: String)= { if (CREATE_LOCALISED_RESOURCE_DOC_JSON_TTL!=0 && Redis.isRedisAvailable()) - jedis.setex(LOCALISED_RESOURCE_DOC_PREFIX+key, CREATE_LOCALISED_RESOURCE_DOC_JSON_TTL, value) + jedisConnection.setex(LOCALISED_RESOURCE_DOC_PREFIX+key, CREATE_LOCALISED_RESOURCE_DOC_JSON_TTL, value) } def getDynamicResourceDocCache(key: String) = { val value = if (GET_DYNAMIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.get(DYNAMIC_RESOURCE_DOC_CACHE_KEY_PREFIX + key) + jedisConnection.get(DYNAMIC_RESOURCE_DOC_CACHE_KEY_PREFIX + key) else null APIUtil.stringOrNone(value) @@ -101,12 +101,12 @@ object Caching extends MdcLoggable { def setDynamicResourceDocCache(key:String, value: String)= { if (GET_DYNAMIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.setex(DYNAMIC_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_DYNAMIC_RESOURCE_DOCS_TTL,value) + jedisConnection.setex(DYNAMIC_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_DYNAMIC_RESOURCE_DOCS_TTL,value) } def getStaticResourceDocCache(key: String) = { val value = if (GET_STATIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.get(STATIC_RESOURCE_DOC_CACHE_KEY_PREFIX + key) + jedisConnection.get(STATIC_RESOURCE_DOC_CACHE_KEY_PREFIX + key) else null APIUtil.stringOrNone(value) @@ -114,12 +114,12 @@ object Caching extends MdcLoggable { def setStaticResourceDocCache(key:String, value: String)= { if (GET_STATIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.setex(STATIC_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_STATIC_RESOURCE_DOCS_TTL,value) + jedisConnection.setex(STATIC_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_STATIC_RESOURCE_DOCS_TTL,value) } def getAllResourceDocCache(key: String) = { val value = if (GET_DYNAMIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.get(ALL_RESOURCE_DOC_CACHE_KEY_PREFIX + key) // null + jedisConnection.get(ALL_RESOURCE_DOC_CACHE_KEY_PREFIX + key) // null else null APIUtil.stringOrNone(value) @@ -127,12 +127,12 @@ object Caching extends MdcLoggable { def setAllResourceDocCache(key:String, value: String)= { if (GET_DYNAMIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.setex(ALL_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_DYNAMIC_RESOURCE_DOCS_TTL,value) + jedisConnection.setex(ALL_RESOURCE_DOC_CACHE_KEY_PREFIX+key,GET_DYNAMIC_RESOURCE_DOCS_TTL,value) } def getStaticSwaggerDocCache(key: String) = { val value = if (GET_STATIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.get(STATIC_SWAGGER_DOC_CACHE_KEY_PREFIX + key) + jedisConnection.get(STATIC_SWAGGER_DOC_CACHE_KEY_PREFIX + key) else null APIUtil.stringOrNone(value) @@ -140,7 +140,7 @@ object Caching extends MdcLoggable { def setStaticSwaggerDocCache(key:String, value: String)= { if (GET_STATIC_RESOURCE_DOCS_TTL!=0 && Redis.isRedisAvailable()) - jedis.setex(STATIC_SWAGGER_DOC_CACHE_KEY_PREFIX+key,GET_STATIC_RESOURCE_DOCS_TTL,value) + jedisConnection.setex(STATIC_SWAGGER_DOC_CACHE_KEY_PREFIX+key,GET_STATIC_RESOURCE_DOCS_TTL,value) } } diff --git a/obp-api/src/main/scala/code/api/cache/Redis.scala b/obp-api/src/main/scala/code/api/cache/Redis.scala index da66487f3..d912d8c74 100644 --- a/obp-api/src/main/scala/code/api/cache/Redis.scala +++ b/obp-api/src/main/scala/code/api/cache/Redis.scala @@ -32,12 +32,29 @@ object Redis extends MdcLoggable { def jedisPoolDestroy: Unit = jedisPool.destroy() val jedisPool = new JedisPool(poolConfig,url, port, 4000) - - lazy val jedis = jedisPool.getResource() + def jedisConnection = jedisPool.getResource() + + def use(method:String, key:String, ttl:Int, value:String) : String = { + var jedisConnection1 = None:Option[Jedis] + try { + jedisConnection1 = Some(jedisPool.getResource()) + if (method=="Get") + jedisConnection1.head.get(key) + else + jedisConnection1.head.setex(key, ttl, value) + } catch { + case e: Throwable => + throw new RuntimeException(e) + } finally { + if(jedisConnection1.isDefined) + jedisConnection1.map(_.close()) + } + } + def isRedisAvailable() = { try { - val status = jedis.isConnected + val status = jedisConnection.isConnected if (!status) { logger.warn("------------| Redis is not connected|------------") } @@ -47,6 +64,8 @@ object Redis extends MdcLoggable { logger.error("------------| Redis throw exception|------------") logger.error(e) false + }finally { + jedisConnection.close() } } diff --git a/obp-api/src/main/scala/code/api/util/RateLimitingUtil.scala b/obp-api/src/main/scala/code/api/util/RateLimitingUtil.scala index 5e780d924..31218ce4d 100644 --- a/obp-api/src/main/scala/code/api/util/RateLimitingUtil.scala +++ b/obp-api/src/main/scala/code/api/util/RateLimitingUtil.scala @@ -1,7 +1,7 @@ package code.api.util import code.api.APIFailureNewStyle -import code.api.cache.Redis.{isRedisAvailable, jedis} +import code.api.cache.Redis.{isRedisAvailable, jedisConnection} import code.api.util.APIUtil.fullBoxOrException import code.api.util.ErrorMessages.TooManyRequests import code.api.util.RateLimitingJson.CallLimit @@ -79,17 +79,17 @@ object RateLimitingUtil extends MdcLoggable { private def underConsumerLimits(consumerKey: String, period: LimitCallPeriod, limit: Long): Boolean = { if (useConsumerLimits) { try { - if (jedis.isConnected() == false) jedis.connect() - (limit, jedis.isConnected()) match { + if (jedisConnection.isConnected() == false) jedisConnection.connect() + (limit, jedisConnection.isConnected()) match { case (_, false) => // Redis is NOT available logger.warn("Redis is NOT available") true case (l, true) if l > 0 => // Redis is available and limit is set val key = createUniqueKey(consumerKey, period) - val exists = jedis.exists(key) + val exists = jedisConnection.exists(key) exists match { case java.lang.Boolean.TRUE => - val underLimit = jedis.get(key).toLong + 1 <= limit // +1 means we count the current call as well. We increment later i.e after successful call. + val underLimit = jedisConnection.get(key).toLong + 1 <= limit // +1 means we count the current call as well. We increment later i.e after successful call. underLimit case java.lang.Boolean.FALSE => // In case that key does not exist we return successful result true @@ -112,25 +112,25 @@ object RateLimitingUtil extends MdcLoggable { private def incrementConsumerCounters(consumerKey: String, period: LimitCallPeriod, limit: Long): (Long, Long) = { if (useConsumerLimits) { try { - if (jedis.isConnected() == false) jedis.connect() - (jedis.isConnected(), limit) match { + if (jedisConnection.isConnected() == false) jedisConnection.connect() + (jedisConnection.isConnected(), limit) match { case (false, _) => // Redis is NOT available logger.warn("Redis is NOT available") (-1, -1) case (true, -1) => // Limit is not set for the period val key = createUniqueKey(consumerKey, period) - jedis.del(key) // Delete the key in accordance to SQL database state. I.e. limit = -1 => delete the key from Redis. + jedisConnection.del(key) // Delete the key in accordance to SQL database state. I.e. limit = -1 => delete the key from Redis. (-1, -1) case _ => // Redis is available and limit is set val key = createUniqueKey(consumerKey, period) - val ttl = jedis.ttl(key).toInt + val ttl = jedisConnection.ttl(key).toInt ttl match { case -2 => // if the Key does not exists, -2 is returned val seconds = RateLimitingPeriod.toSeconds(period).toInt - jedis.setex(key, seconds, "1") + jedisConnection.setex(key, seconds, "1") (seconds, 1) case _ => // otherwise increment the counter - val cnt = jedis.incr(key) + val cnt = jedisConnection.incr(key) (ttl, cnt) } } @@ -146,7 +146,7 @@ object RateLimitingUtil extends MdcLoggable { private def ttl(consumerKey: String, period: LimitCallPeriod): Long = { val key = createUniqueKey(consumerKey, period) - val ttl = jedis.ttl(key).toInt + val ttl = jedisConnection.ttl(key).toInt ttl match { case -2 => // if the Key does not exists, -2 is returned 0 @@ -161,12 +161,12 @@ object RateLimitingUtil extends MdcLoggable { def getInfo(consumerKey: String, period: LimitCallPeriod): ((Option[Long], Option[Long]), LimitCallPeriod) = { val key = createUniqueKey(consumerKey, period) - val ttl = jedis.ttl(key).toLong + val ttl = jedisConnection.ttl(key).toLong ttl match { case -2 => ((None, None), period) case _ => - ((Some(jedis.get(key).toLong), Some(ttl)), period) + ((Some(jedisConnection.get(key).toLong), Some(ttl)), period) } } diff --git a/obp-api/src/test/scala/code/setup/LocalMappedConnectorTestSetup.scala b/obp-api/src/test/scala/code/setup/LocalMappedConnectorTestSetup.scala index 943b1b71d..d183bf265 100644 --- a/obp-api/src/test/scala/code/setup/LocalMappedConnectorTestSetup.scala +++ b/obp-api/src/test/scala/code/setup/LocalMappedConnectorTestSetup.scala @@ -181,8 +181,8 @@ trait LocalMappedConnectorTestSetup extends TestConnectorSetupWithStandardPermis // Flush all data from Redis try { - Redis.jedis.connect() - Redis.jedis.flushDB() + Redis.jedisConnection.connect() + Redis.jedisConnection.flushDB() } catch { case e: Throwable => logger.warn("------------| Redis issue during flushing data |------------")