AMAZON ElastiCache(1)Redis Basic Operation and Testing

本文介绍使用Amazon ElastiCache Redis进行基本操作及测试的方法,包括配置和连接Redis集群、实现读写分离,并通过Scala语言进行客户端编程。此外,还提供了一个测试类以验证Redis客户端的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AMAZON ElastiCache(1)Redis Basic Operation and Testing

We are using AMAZON Server which is not Redis 3.0 yet, the latest is 2.8.x. So it is not really Redis Cluster, it is master and slave. And we can only balance the read operation on the slaves.

Finally, my colleague who is a really smart people find one scala driver for me.

build.sbt
"com.etaty.rediscala" %% "rediscala" % "1.4.2”, //scala drvier
"com.orange.redis-embedded" % "embedded-redis" % "0.6" % “test” //embedded server

And there is only one RedisElastiCache.scala core implementation
package com.sillycat.jobsconsumer.persistence

import akka.actor.ActorSystem
import akka.util.ByteString
import com.sillycat.jobsconsumer.utilities.{IncludeConfig, IncludeLogger}
import redis.{RedisClientMasterSlaves, RedisServer}
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.collection.JavaConverters._

object RedisElastiCache extends IncludeLogger with IncludeConfig{

implicit var actorSystem: ActorSystem = _

private val cachePool = {
try {
logger.info("Init the Akka System.")
actorSystem = ActorSystem("RedisScalaClients")

val masterHost = config.getString(envStr("redis.master"))
logger.info("Connect to master host = " + masterHost)
val master = RedisServer(masterHost)

val slaves = config.getStringList(envStr("redis.slaves")).asScala map { case slaveHost: String =>
logger.info("Connect to slave host = " + slaveHost)
RedisServer(slaveHost)
}

RedisClientMasterSlaves(master,slaves.toSeq)
} catch {
case x: Throwable =>
logger.error("Couldn't connect to ElastiCache: " + x)
null
}
}

def releaseResource = {
if(actorSystem != null){
actorSystem.shutdown()
}
}

def get(key: String):Option[Any] = {
val future = cachePool.get(key)
future onFailure {
case e => {
logger.error("Fetch to fetch the value from ElastiCache: " + e.getMessage)
}
}
Await.result(future, 5 seconds) match {
case Some(v:ByteString) => {
logger.debug("Receiving the result from remote: " + new String(v.toArray))
Some(new String(v.toArray))
}
case _ => {
logger.error("Get Wrong format from ElastiCache.")
None
}
}
}

def put(key:String, value:Any) = {
val future = cachePool.set(key, value.toString)
future onFailure {
case e => logger.error("Fetch to put the value to ElastiCache: " + e.getMessage)
}
Await.result(future, 5 seconds)
}

}

Here is the test class RedisElastiCacheSpec.scala
package com.sillycat.jobsconsumer.persistence

import com.sillycat.jobsconsumer.utilities.IncludeConfig
import org.scalatest.{Matchers, FunSpec}
import org.scalatest.BeforeAndAfterAll
import redis.embedded.RedisServer

/**
* Created by carl on 7/31/15.
*/
class RedisElastiCacheSpec extends FunSpec with Matchers with BeforeAndAfterAll with IncludeConfig{

var redisEmbeded :RedisServer = _

override def beforeAll() {
if(config.getString("build.env").equals("test")){
redisEmbeded = new RedisServer(6379)
redisEmbeded.start()
}
}

override def afterAll() {
RedisElastiCache.releaseResource
if(redisEmbeded != null){
redisEmbeded.stop()
}
}

describe("RedisElastiCache") {
describe("#put & get"){
it("Put String to ElastiCache") {
val expect = "<job>sdfasdf</job>"
RedisElastiCache.put("key1",expect)
val result = RedisElastiCache.get("key1")
result should be (Some(expect))
}
}
}
}


References:
http://maciejb.me/2012/10/17/testing-your-java-amazon-sqs-code-with-elasticmq/
https://github.com/kstyrc/embedded-redis
http://msgpack.org/
http://alvinalexander.com/scala/serializing-deserializing-xml-scala-classes

https://github.com/xetorthio/jedis

master and slave
http://etaty.github.io/rediscala/latest/api/index.html#redis.RedisClientMasterSlaves
https://github.com/debop/debop4s/blob/36ab5a45181e03022d5b219ac0dec5bcaed714b6/debop4s-rediscala/src/test/scala/debop4s/rediscala/client/MasterSlavesFunSuite.scala

embeded server for testing
https://github.com/kstyrc/embedded-redis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值