scala对redis集群操作工具类 附带java版本的工具类以及spring配置

  • Scala的工具类如下:
package cn.lijie


import java.util

import redis.clients.jedis.{HostAndPort, JedisCluster}

import scala.collection.JavaConversions._
import scala.collection.mutable

/**
  * User: lijie
  */
object RedisClient {

  val clients = RedisConnector.clients

  /**
    *
    * @param key
    * @param value
    * @return
    */
  def set(key: String, value: String): Unit = {
    clients.set(key, value)
  }

  /**
    *
    * @param key
    * @return
    */
  def get(key: String): Option[String] = {
    val value = clients.get(key)
    if (value == null)
      None
    else
      Some(value)
  }


  /**
    *
    * @param key
    */
  def del(key: String): Unit = {
    clients.del(key)
  }

  /**
    *
    * @param hkey
    * @param key
    * @param value
    * @return
    */
  def hset(hkey: String, key: String, value: String): Boolean = {
    clients.hset(hkey, key, value) == 1
  }

  /**
    *
    * @param hkey
    * @param key
    * @return
    */
  def hget(hkey: String, key: String): Option[String] = {

    val value = clients.hget(hkey, key)
    if (value == null)
      None
    else
      Some(value)

  }

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

    Some(clients.hdel(hkey, key))

  }

  /**
    *
    * @param hkey
    * @param map
    */
  def hmset(hkey: String, map: mutable.Map[String, String]): Unit = {
    clients.hmset(hkey, mapAsJavaMap(map))
  }

  /**
    *
    * @param key
    * @param value
    * @return
    */
  def rpush(key: String, value: String): Option[Long] = {
    Some(clients.rpush(key, value))
  }


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

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


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

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

  /**
    *
    * @param key
    * @return
    */
  def incr(key: String): Option[Long] = {
    val inc = clients.incr(key)
    if (inc == null)
      None
    else
      Some(inc)
  }

  /**
    *
    * @param key
    * @param time
    * @return
    */
  def expire(key: String, time: Int) = {
    clients.expire(key, time)
  }

  /**
    *
    * @param key
    * @return
    */
  def ttl(key: String): Option[Long] = {
    Some(clients.ttl(key))
  }


}

object RedisConnector {

  private val jedisClusterNodes = new util.HashSet[HostAndPort]()
  jedisClusterNodes.add(new HostAndPort("10.0.4.140", 7000))
  jedisClusterNodes.add(new HostAndPort("10.0.4.141", 7001))
  jedisClusterNodes.add(new HostAndPort("10.0.4.142", 7002))
  jedisClusterNodes.add(new HostAndPort("10.0.4.143", 7003))
  jedisClusterNodes.add(new HostAndPort("10.0.4.144", 7004))

  val clients = new JedisCluster(jedisClusterNodes)
}


object MainClass {
  def main(args: Array[String]): Unit = {
    val dao = RedisClient
    //    dao.set("lijie", "123")
    //    dao.del("lijie")
    //    println(dao.get("lijie"))
    //    dao.hset("myhset", "lijie1", "chongqing")
    //    dao.hset("myhset", "lijie2", "shenzheng")
    //    println(dao.hget("myhset", "lijie1"))
    //    dao.hdel("myhset", "lijie1")
    //    dao.rpush("myfifo", "a")
    //    dao.rpush("myfifo", "c")
    //    dao.rpush("myfifo", "b")
    //
    //    println(dao.lhead("myfifo"))
    //
    //    println(dao.lpop("myfifo"))
    //    println(dao.lpop("myfifo"))
    //    println(dao.lpop("myfifo"))

    //    println(dao.incr("myincr"))

  }
}

java版本的如下:
其中JedisClient是一个总接口,单机版和集群版本都实现了他的方法

  • JedisClient:

/**
 * 
 * @author 黎杰
 */
public interface JedisClient {

    String get(String key);

    String set(String key, String value);

    String hget(String hkey, String key);

    long hset(String hkey, String key, String value);

    long incr(String key);

    long expire(String key, int second);

    long ttl(String key);

    long del(String key);

    long hdel(String hkey, String key);

}
  • JedisClientCluster:

import org.springframework.beans.factory.annotation.Autowired;

import cn.ecar.web.dao.redisdao.JedisClient;
import redis.clients.jedis.JedisCluster;

/**
 * 
 * @author 黎杰
 */
public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String hget(String hkey, String key) {
        return jedisCluster.hget(hkey, key);
    }

    @Override
    public long hset(String hkey, String key, String value) {
        return jedisCluster.hset(hkey, key, value);
    }

    @Override
    public long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public long expire(String key, int second) {
        return jedisCluster.expire(key, second);
    }

    @Override
    public long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public long del(String key) {

        return jedisCluster.del(key);
    }

    @Override
    public long hdel(String hkey, String key) {

        return jedisCluster.hdel(hkey, key);
    }

}
  • JedisClientSingle:

import org.springframework.beans.factory.annotation.Autowired;

import cn.ecar.web.dao.redisdao.JedisClient;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author 黎杰
 */
public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    @Override
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    @Override
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    @Override
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

    @Override
    public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey, key);
        jedis.close();
        return result;
    }

}
  • java版本spring的配置:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxTotal" value="30" />
        
        <property name="maxIdle" value="10" />
        
        <property name="numTestsPerEvictionRun" value="1024" />
        
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        
        <property name="maxWaitMillis" value="1500" />
        
        <property name="testOnBorrow" value="true" />
        
        <property name="testWhileIdle" value="true" />
        
        <property name="blockWhenExhausted" value="false" />
    bean>
    
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.80.123">constructor-arg>
        <constructor-arg name="port" value="6379">constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
    bean>
    <bean id="jedisClient" class="cn.ecar.web.dao.redisdao.impl.JedisClientSingle" />


    
    
beans>
  • pom:
        
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.7.3version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
            <version>2.2version>
        dependency>

你可能感兴趣的:(Java,scala)