zoukankan      html  css  js  c++  java
  • Java代码redis基础操作

    maven依赖包:

     1         <dependency>
     2             <groupId>redis.clients</groupId>
     3             <artifactId>jedis</artifactId>
     4             <version>2.9.0</version>
     5         </dependency>
     6         <dependency>
     7             <groupId>org.apache.commons</groupId>
     8             <artifactId>commons-pool2</artifactId>
     9             <version>2.6.0</version>
    10         </dependency>

    源码:

     1 package StudyPro.base;
     2 
     3 import org.junit.Test;
     4 import redis.clients.jedis.Jedis;
     5 import redis.clients.jedis.JedisPool;
     6 import redis.clients.jedis.JedisPoolConfig;
     7 
     8 public class TestSample {
     9 //    Jedis jedis = new Jedis("10.15.1.19",16379);
    10 
    11     // 简单连接
    12     @Test
    13     public void test() {
    14         Jedis jedis = new Jedis("10.15.1.19", 16379);
    15         jedis.auth("wC3Xo8E5mlmgMb");
    16         jedis.set("a", "bb");
    17         System.out.println(jedis.get("a"));
    18         jedis.close();
    19     }
    20 
    21     // 使用连接池
    22     @Test
    23     public void testPool() {
    24         JedisPoolConfig poolConfig = new JedisPoolConfig();
    25         poolConfig.setMaxIdle(20);
    26         poolConfig.setMinIdle(10);
    27         poolConfig.setMaxTotal(30);
    28         poolConfig.setMaxWaitMillis(3000);
    29         poolConfig.setTestOnBorrow(true);
    30         poolConfig.setTestOnReturn(true);
    31         JedisPool jedisPool = new JedisPool("10.15.1.19", 16379);
    32         Jedis jedis = jedisPool.getResource();
    33         jedis.auth("wC3Xo8E5mlmgMb");
    34         System.out.println(jedis.get("a"));
    35         jedis.close();
    36         jedisPool.close();
    37     }
    38 
    39 }
  • 相关阅读:
    PHP如何解决网站大流量与高并发的问题
    HTML CSS 特殊字符表
    PHP 把返回的数据集转换成Tree树
    网页加载时域名加载数量限制
    关于浮点型误差的解决方法
    MVC三层架构
    MyBatis
    plsql储存过程
    游标和触发器
    使用plsql编程
  • 原文地址:https://www.cnblogs.com/gongxr/p/9406695.html
Copyright © 2011-2022 走看看