scala使用redis client - Jedis

1 篇文章 0 订阅

虽然scala也有很多redis的client,不过我在使用最新的Redis3.0集群模式的时候总是报错,只好用回java版本的client了。

报错如下

java.lang.Exception: MOVED 1133 192.168.6.53:6379

代码

package cn.www.dao

import java.util

import cn.www.dao.RedisConnector.clients
import redis.clients.jedis.{JedisCluster, HostAndPort}
import scala.collection.JavaConversions._

import scala.collection.mutable


class RedisDao extends Serializable{

  /**
   *
   * @param key
   * @param value
   * @return
   */

  def set(key:String,value:Any): Unit ={

    clients.set(key,String.valueOf(value))

  }

  /**
   *
   * @param key
   * @param time
   * @return
   */

  def hget(key:String,time:Long): Option[String] ={

    val value=clients.hget(key,String.valueOf(time))
    if(value==null)
      None
    else
      Some(value)

  }

  /**
   *
   * @param key
   * @param time
   * @param value
   * @return
   */
  def hset(key:String,time:Long,value:Any): Boolean ={
    clients.hset(key,String.valueOf(time),String.valueOf(value))==1

  }

  def hmset(key:String,map:mutable.Map[Long,String]): Unit ={
    val map2=mutable.Map[String,String]()
    map.foreach{case (key:Long,value:String)=>
      map2.put(key.toString,value)
    }

    clients.hmset(key,mapAsJavaMap(map2))

  }

  /**
   *
   * @param key
   * @param time
   * @return
   */
  def hdel(key:String,time:Any): Option[Long] ={

    Some(clients.hdel(key,String.valueOf(time)))

  }

  /**
   *
   * @param key
   * @param times
   * @return
   */
  def rpush(key:String,times:Any): Option[Long] ={

    Some(clients.rpush(key,String.valueOf(times)))

  }


  /**
   *
   * @param key
   * @return
   */
  def lpop(key:String): Option[Long] ={

    val time=clients.lpop(key)
    if(time==null)
      None
    else
      Some(time.toLong)
  }


  /**
   *
   * @param key
   * @return
   */
  def lhead(key:String): Option[Long] ={

    val head=clients.lindex(key,0)
    if(head==null)
      None
    else
      Some(head.toLong)
  }

}

object RedisConnector {

  private val jedisClusterNodes = new util.HashSet[HostAndPort]()
  jedisClusterNodes.add(new HostAndPort("192.168.6.52",6379))
  jedisClusterNodes.add(new HostAndPort("192.168.6.53",6380))
  jedisClusterNodes.add(new HostAndPort("192.168.6.53",6379))

  val clients = new JedisCluster(jedisClusterNodes)

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值