AMAZON ElastiCache(1)Redis Basic Operation and Testing

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

猜你喜欢

转载自sillycat.iteye.com/blog/2232611