一.windows下安装redis
(1)安装地址:https://github.com/MicrosoftArchive/redis/releases
选择.msi结尾的,直接安装
(2)启动服务端
命令:redis-server.exe redis.windows.conf
(3)启动客户端
刚才启动的不要关了,重新打开个命令窗口
二.基本使用
set admin 123
get admin
查看所有key:keys *
三.代码
(1)pom.xml
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.1.0</version> </dependency>
(2)JedisDemo1.java
package cn.javacat; import redis.clients.jedis.Jedis; public class JedisDemo1 { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); jedis.set("userName", "张三"); String userName = jedis.get("userName"); String test = jedis.get("test"); System.out.println(userName); System.out.println(test); jedis.close(); } }
运行结果:
(3)连接池demo
1 package cn.javacat; 2 3 import redis.clients.jedis.Jedis; 4 import redis.clients.jedis.JedisPool; 5 import redis.clients.jedis.JedisPoolConfig; 6 7 public class JedisPoolTest { 8 public static void main(String[] args) { 9 JedisPoolConfig config = new JedisPoolConfig(); 10 // 设置最大连接数 11 config.setMaxTotal(100); 12 // 设置最大空闲连接数 13 config.setMaxIdle(10); 14 15 JedisPool jedisPool = new JedisPool("127.0.0.1", 6379); 16 Jedis jedis = null; 17 try { 18 jedis = jedisPool.getResource(); 19 jedis.set("password", "123456"); 20 String password = jedis.get("password"); 21 String test = jedis.get("test"); 22 System.out.println(password); 23 System.out.println(test); 24 jedis.close(); 25 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } finally { 29 if (jedis != null) { 30 jedis.close(); 31 } 32 if (jedisPool != null) { 33 jedisPool.close(); 34 } 35 } 36 37 } 38 39 }