Merge pull request #2301 from constantine2nd/develop

Cache, Redis mock
This commit is contained in:
Simon Redfern 2023-10-20 15:04:33 +02:00 committed by GitHub
commit 9e8e36aa23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 14 deletions

View File

@ -316,10 +316,11 @@
<artifactId>scalameta_${scala.version}</artifactId>
<version>3.7.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.codemonstur/embedded-redis -->
<dependency>
<groupId>ai.grakn</groupId>
<artifactId>redis-mock</artifactId>
<version>0.1.6</version>
<groupId>com.github.codemonstur</groupId>
<artifactId>embedded-redis</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.nimbusds/oauth2-oidc-sdk -->

View File

@ -14,8 +14,8 @@ import scala.language.postfixOps
object Redis extends MdcLoggable {
val url = APIUtil.getPropsValue("guava.cache.url", "127.0.0.1")
val port = APIUtil.getPropsAsIntValue("guava.cache.port", 6379)
val url = APIUtil.getPropsValue("guava.redis.url", "127.0.0.1")
val port = APIUtil.getPropsAsIntValue("guava.redis.port", 6379)
implicit val scalaCache = ScalaCache(RedisCache(url, port))
implicit val flags = Flags(readsEnabled = true, writesEnabled = true)

View File

@ -9,9 +9,11 @@ import com.openbankproject.commons.model.User
import net.liftweb.common.{Box, Empty, Full}
import net.liftweb.util.Props
import redis.clients.jedis.Jedis
import redis.embedded.RedisServer
import scala.collection.immutable
import scala.collection.immutable.{List, Nil}
import scala.util.Random
object RateLimitingPeriod extends Enumeration {
@ -71,6 +73,13 @@ object RateLimitingJson {
object RateLimitingUtil extends MdcLoggable {
import code.api.util.RateLimitingPeriod._
val port = APIUtil.getPropsAsIntValue("redis_port", 6379)
val url = APIUtil.getPropsValue("redis_address", "127.0.0.1")
private val mockedRedisPort = 6380 + Random.nextInt(20)
private val mockedRedisHost = "127.0.0.1"
private var server: RedisServer = null
def useConsumerLimits = APIUtil.getPropsAsBoolValue("use_consumer_limits", false)
def inMemoryMode = APIUtil.getPropsAsBoolValue("use_consumer_limits_in_memory_mode", false)
@ -82,21 +91,18 @@ object RateLimitingUtil extends MdcLoggable {
if(inMemoryMode == true) {
startMockedRedis(mode="In-Memory")
} else {
val port = APIUtil.getPropsAsIntValue("redis_port", 6379)
val url = APIUtil.getPropsValue("redis_address", "127.0.0.1")
new Jedis(url, port)
}
}
private def startMockedRedis(mode: String): Jedis = {
import ai.grakn.redismock.RedisServer
import redis.clients.jedis.Jedis
val server = RedisServer.newRedisServer // bind to a random port
server = new RedisServer(mockedRedisPort)
server.start()
logger.info(msg = "-------------| Mocked Redis instance has been run in " + mode + " mode")
logger.info(msg = "-------------| at host: " + server.getHost)
logger.info(msg = "-------------| at port: " + server.getBindPort)
new Jedis(server.getHost, server.getBindPort)
logger.info(msg = "-------------| Mocked Redis instance has been run in " + mode + " mode")
logger.info(msg = "-------------| at host: " + mockedRedisHost)
logger.info(msg = "-------------| at port: " + mockedRedisPort)
new Jedis(mockedRedisHost, mockedRedisPort)
}
def isRedisAvailable() = {
@ -123,7 +129,7 @@ object RateLimitingUtil extends MdcLoggable {
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
case (l, true) if l >= 0 => // Redis is available and limit is set
val key = createUniqueKey(consumerKey, period)
val exists = jedis.exists(key)
exists match {