java连接Redis
1、安装redis(windows)
下载连接:https://github.com/microsoftarchive/redis/releases
2、启动redis
3、创建maven工程,引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
4、编写java代码
package com.cn.fb.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisUtil {
public static void main(String[] args) {
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.set("name", "admin");
System.out.println(jedis.get("name"));
jedis.close();
}
public static Jedis cliSingle(String host, int port) {
try {
return new Jedis(host, port);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Jedis cliPool(String host, int port) {
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(2);
JedisPool jedisPool = new JedisPool(config, host, port);
return jedisPool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
5、使用Redis Desktop Manager连接Redis查看插入的key value值
|