Scala Jedis 基本使用

依赖添加

因为要使用 scala 来演示怎样使用 Jedis,所以在build.sbt上加入以下依赖库信息。

"redis.clients" % "jedis" % "2.9.0",

样例代码

Scala 调用 Jedis 代码如下:

import redis.clients.jedis.Jedis
object SampleJedis {
    def main(args: Array[String]): Unit = {
        withRedis { jedis =>
            println(jedis.set("test1", "001"))
            println(jedis.get("test1"))
        }
    }
    def withRedis[T](f: Jedis => T): T = {
        val jedis = new Jedis("localhost", 9092)
        try {
            f(jedis)
        } finally {
            jedis.close()
        }
    }
}

标准输出:

OK
001

Jedis 并不是线程安全的,所以不能多个线程共用一个 Jedis 实例,但是创建多个 Jedis 实例供多个线程使用则需要建立多个 Socket。

JedisPool 是一个基于 apache commons-pool 的线程安全的网络连接池,可以创建多个可靠的 Jedis 实例,在需要的时候从连接池中取出 Jedis 实例,使用完后再返还给 JedisPool。这种方式可以有效的提高应用的通信能力和性能,并且这是一种良好的设计模式。

Scala 调用 Jedis Pool 代码如下:

import org.apache.log4j.{Level, Logger}
import redis.clients.jedis.{JedisPoolConfig, Jedis, JedisPool}

object RedisSingle {
  def main(args: Array[String]) {
    if (args.length < 3) {
      System.err.println(s"""
        |Usage: <IP> <Port> <Auth>
        """.stripMargin)
      System.exit(1)
    }
    // Scala里的数组是通过把索引放在圆括号里面访问的
    var ip = args(0)
    var port = args(1).toInt
    var auth = args(2)

    Logger.getLogger("org").setLevel(Level.OFF)
    Logger.getLogger("akka").setLevel(Level.OFF)

    val config: JedisPoolConfig = new JedisPoolConfig
    config.setMaxTotal(8) //最大空闲连接数, 默认8个
    config.setMaxIdle(8)  //最大连接数, 默认8个
    // timeout: 连接 redis 时候的超时
    // JedisPool(jedisPoolConfig, redisHost, redisPort, timeout, redisPassword)
     var pool = new JedisPool(config, ip, port, 3000, auth)
    //var pool = new JedisPool(config, ip, port, 3000)
    var rds: Jedis = null
    try {
      rds = pool.getResource()
      println(rds.set("test1", "001"))
      println(rds.get("test1"))
    } catch {
      case e: Exception => {
        println("jedis error:E" + e)
      }
    } finally {
      if (rds != null) {
        // pool.returnResource(rds)
        /*在 jedis2.8.2 版本中已将 JedisPool#returnResource 方法废弃, 
        并明确说明这个方法的功能由Jedis.close() 方法代替*/ 
        rds.close()
      }
    }
    pool.close()
  }
}

本文链接:/2018/03/31/jedis/
请尊重作者的劳动成果,转载请注明出处!Sakishum 博客保留对文章的所有权利。

Pingbacks已关闭。

评论
  1. RebeccaJuicy RebeccaJuicy 发布于 2018年9月28日 21:08 #

    Hi. I see that you don't update your page too often. I know that writing content is boring and time consuming.
    But did you know that there is a tool that allows you to
    create new posts using existing content (from article directories
    or other pages from your niche)? And it does it very well.
    The new posts are high quality and pass the copyscape test.

    Search in google and try: miftolo's tools

  2. RebeccaJuicy RebeccaJuicy 发布于 2018年9月28日 21:08 #

    Hi. I see that you don't update your page too often. I know that writing content is boring
    and time consuming. But did you know that there is a tool that allows you to create new posts using existing content (from article directories or other
    pages from your niche)? And it does it very well. The new
    posts are high quality and pass the copyscape
    test. Search in google and try: miftolo's tools

  3. EllLish EllLish 发布于 2019年1月14日 14:11 #

    Does Amoxicillin Treat Stds Amoxicillin Generique Suisse <a href=http://drugslr.com>cheapest cialis 20mg</a> Buy Amoxicillin Online Buy Amoxicillin 500mg Capsules Erfahrungen Mit Viagra Zithromax Testicle Pain Cytotec 200 РћСљg <a href=http://truthaboutstaininggrid.com></a> Priligy Acquisto On Line Prednisone Canadian Pharmnacy

发表评论