Home Java javaTutorial Sample code for using Jedis to operate Redis in Java

Sample code for using Jedis to operate Redis in Java

Jan 21, 2017 pm 04:26 PM

Using Java to operate Redis requires jedis-2.1.0.jar, download address: jedis-2.1.0.jar

If you need to use the Redis connection pool, you also need commons-pool-1.5.4. jar, download address: commons-pool-1.5.4.jar

package com.test;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.junit.Before;
import org.junit.Test;
 
import redis.clients.jedis.Jedis;
 
public class TestRedis {
  private Jedis jedis; 
   
  @Before
  public void setup() {
    //连接redis服务器,192.168.0.100:6379
    jedis = new Jedis("192.168.0.100", 6379);
    //权限认证
    jedis.auth("admin"); 
  }
   
  /**
   * redis存储字符串
   */
  @Test
  public void testString() {
    //-----添加数据---------- 
    jedis.set("name","xinxin");//向key-->name中放入了value-->xinxin 
    System.out.println(jedis.get("name"));//执行结果:xinxin 
     
    jedis.append("name", " is my lover"); //拼接
    System.out.println(jedis.get("name")); 
     
    jedis.del("name"); //删除某个键
    System.out.println(jedis.get("name"));
    //设置多个键值对
    jedis.mset("name","liuling","age","23","qq","476777XXX");
    jedis.incr("age"); //进行加1操作
    System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"));
     
  }
   
  /**
   * redis操作Map
   */
  @Test
  public void testMap() {
    //-----添加数据---------- 
    Map<String, String> map = new HashMap<String, String>();
    map.put("name", "xinxin");
    map.put("age", "22");
    map.put("qq", "123456");
    jedis.hmset("user",map);
    //取出user中的name,执行结果:[minxr]-->注意结果是一个泛型的List 
    //第一个参数是存入redis中map对象的key,后面跟的是放入map中的对象的key,后面的key可以跟多个,是可变参数 
    List<String> rsmap = jedis.hmget("user", "name", "age", "qq");
    System.out.println(rsmap); 
  
    //删除map中的某个键值 
    jedis.hdel("user","age");
    System.out.println(jedis.hmget("user", "age")); //因为删除了,所以返回的是null 
    System.out.println(jedis.hlen("user")); //返回key为user的键中存放的值的个数2 
    System.out.println(jedis.exists("user"));//是否存在key为user的记录 返回true 
    System.out.println(jedis.hkeys("user"));//返回map对象中的所有key 
    System.out.println(jedis.hvals("user"));//返回map对象中的所有value 
  
    Iterator<String> iter=jedis.hkeys("user").iterator(); 
    while (iter.hasNext()){ 
      String key = iter.next(); 
      System.out.println(key+":"+jedis.hmget("user",key)); 
    } 
  }
   
  /** 
   * jedis操作List 
   */
  @Test
  public void testList(){ 
    //开始前,先移除所有的内容 
    jedis.del("java framework"); 
    System.out.println(jedis.lrange("java framework",0,-1)); 
    //先向key java framework中存放三条数据 
    jedis.lpush("java framework","spring"); 
    jedis.lpush("java framework","struts"); 
    jedis.lpush("java framework","hibernate"); 
    //再取出所有数据jedis.lrange是按范围取出, 
    // 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 
    System.out.println(jedis.lrange("java framework",0,-1)); 
     
    jedis.del("java framework");
    jedis.rpush("java framework","spring"); 
    jedis.rpush("java framework","struts"); 
    jedis.rpush("java framework","hibernate"); 
    System.out.println(jedis.lrange("java framework",0,-1));
  } 
   
  /** 
   * jedis操作Set 
   */
  @Test
  public void testSet(){ 
    //添加 
    jedis.sadd("user","liuling"); 
    jedis.sadd("user","xinxin"); 
    jedis.sadd("user","ling"); 
    jedis.sadd("user","zhangxinxin");
    jedis.sadd("user","who"); 
    //移除noname 
    jedis.srem("user","who"); 
    System.out.println(jedis.smembers("user"));//获取所有加入的value 
    System.out.println(jedis.sismember("user", "who"));//判断 who 是否是user集合的元素 
    System.out.println(jedis.srandmember("user")); 
    System.out.println(jedis.scard("user"));//返回集合的元素个数 
  } 
  
  @Test
  public void test() throws InterruptedException { 
    //jedis 排序 
    //注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的) 
    jedis.del("a");//先清除数据,再加入数据进行测试 
    jedis.rpush("a", "1"); 
    jedis.lpush("a","6"); 
    jedis.lpush("a","3"); 
    jedis.lpush("a","9"); 
    System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] 
    System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //输入排序后结果 
    System.out.println(jedis.lrange("a",0,-1)); 
  } 
   
  @Test
  public void testRedisPool() {
    RedisUtil.getJedis().set("newname", "中文测试");
    System.out.println(RedisUtil.getJedis().get("newname"));
  }
}
Copy after login

Redis connection pool:

package com.test;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
public final class RedisUtil {
   
  //Redis服务器IP
  private static String ADDR = "192.168.0.100";
   
  //Redis的端口号
  private static int PORT = 6379;
   
  //访问密码
  private static String AUTH = "admin";
   
  //可用连接实例的最大数目,默认值为8;
  //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
  private static int MAX_ACTIVE = 1024;
   
  //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
  private static int MAX_IDLE = 200;
   
  //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
  private static int MAX_WAIT = 10000;
   
  private static int TIMEOUT = 10000;
   
  //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
  private static boolean TEST_ON_BORROW = true;
   
  private static JedisPool jedisPool = null;
   
  /**
   * 初始化Redis连接池
   */
  static {
    try {
      JedisPoolConfig config = new JedisPoolConfig();
      config.setMaxActive(MAX_ACTIVE);
      config.setMaxIdle(MAX_IDLE);
      config.setMaxWait(MAX_WAIT);
      config.setTestOnBorrow(TEST_ON_BORROW);
      jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
   
  /**
   * 获取Jedis实例
   * @return
   */
  public synchronized static Jedis getJedis() {
    try {
      if (jedisPool != null) {
        Jedis resource = jedisPool.getResource();
        return resource;
      } else {
        return null;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
   
  /**
   * 释放jedis资源
   * @param jedis
   */
  public static void returnResource(final Jedis jedis) {
    if (jedis != null) {
      jedisPool.returnResource(jedis);
    }
  }
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that Please support PHP Chinese website.

For more sample code related articles using Jedis to operate Redis in Java, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles