一.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
package cn.javacat; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolTest { public static void main(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); // 设置最大连接数 config.setMaxTotal(100); // 设置最大空闲连接数 config.setMaxIdle(10); JedisPool jedisPool = new JedisPool("127.0.0.1", 6379); Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set("password", "123456"); String password = jedis.get("password"); String test = jedis.get("test"); System.out.println(password); System.out.println(test); jedis.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (jedis != null) { jedis.close(); } if (jedisPool != null) { jedisPool.close(); } } } }