redis-server.exe redis.windows.conf
redis-cli.exe -h 127.0.0.1 -p 6379
config get requirepass
config set requirepass 123456
auth 123456
keys *查看
然后在Java中连接Redis
pom
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
RedisUtils
package cn.jiedada.hrm.util; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; /** * 获取连接池对象 */ public enum RedisUtils { INSTANCE; //连接池 static JedisPool jedisPool = null; static { //1 创建连接池配置对象 JedisPoolConfig config = new JedisPoolConfig(); //2 进行配置-四个配置 config.setMaxIdle(1);//最小连接数 config.setMaxTotal(11);//最大连接数 config.setMaxWaitMillis(10 * 1000L);//最长等待时间 config.setTestOnBorrow(true);//测试连接时是否畅通 //3 通过配置对象创建连接池对象 Properties properties = null; try { properties = new Properties(); properties.load(RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties")); } catch (IOException e) { e.printStackTrace(); } String host = properties.getProperty("redis.host"); String port = properties.getProperty("redis.port"); String password = properties.getProperty("redis.password"); String timeout = properties.getProperty("redis.timeout"); jedisPool = new JedisPool(config, host, Integer.valueOf(port),Integer.valueOf(timeout), password); } //获取连接 public Jedis getSource() { return jedisPool.getResource(); } //关闭资源 public void closeSource(Jedis jedis) { if (jedis != null) { jedis.close(); } } /** * hash操作字符串============================================== */ public List<String> hvals(String key) { Jedis jedis = getSource(); List<String> hvals = jedis.hvals(key); closeSource(jedis); return hvals; } public Set<String> hkeys(String key) { Jedis jedis = getSource(); Set<String> hkeys = jedis.hkeys(key); closeSource(jedis); return hkeys; } public Long hset(String key,String field,String value) { Jedis jedis = getSource(); Long result = jedis.hset(key,field,value); closeSource(jedis); return result; } public String hmset(String key, Map<String,String> data) { Jedis jedis = getSource(); String result = jedis.hmset(key,data); closeSource(jedis); return result; } public String hget(String key,String field) { Jedis jedis = getSource(); String value = jedis.hget(key, field); closeSource(jedis); return value; } /** * hash操作byte[]============================================== */ public List<byte[]> hvals(byte[] key) { Jedis jedis = getSource(); List<byte[]> hvals = jedis.hvals(key); closeSource(jedis); return hvals; } public Set<byte[]> hkeys(byte[] key) { Jedis jedis = getSource(); Set<byte[]> hkeys = jedis.hkeys(key); closeSource(jedis); return hkeys; } public Long hset(byte[] key,byte[] field,byte[] value) { Jedis jedis = getSource(); Long result = jedis.hset(key,field,value); closeSource(jedis); return result; } public String hmset(byte[] key, Map<byte[],byte[]> data) { Jedis jedis = getSource(); String result = jedis.hmset(key,data); closeSource(jedis); return result; } public byte[] hget(byte[] key,byte[] field) { Jedis jedis = getSource(); byte[] value = jedis.hget(key, field); closeSource(jedis); return value; } /** * 删除key */ public boolean del(String key) { Jedis jedis = getSource(); long result = jedis.del(key); closeSource(jedis); return result > 0; } /** * 设置字符值 * * @param key * @param value :value是字符串,如果要存储对象,转成JSON字符串在存储 */ public void set(String key, String value) { Jedis jedis = getSource(); jedis.set(key, value); closeSource(jedis); } /** * 设置 * @param key * @param value */ public void set(byte[] key, byte[] value) { Jedis jedis = getSource(); jedis.set(key, value); closeSource(jedis); } /** * * @param key * @return */ public byte[] get(byte[] key) { Jedis jedis = getSource(); try { return jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { closeSource(jedis); } return null; } /** * 设置字符值 * * @param key * @return :返回的是JSON格式的字符串 ,考虑转对象 */ public String get(String key) { Jedis jedis = getSource(); try { return jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { closeSource(jedis); } return null; } }
配置类
redis.properties
redis.host=127.0.0.1 redis.port=6379 redis.password=123456 redis.timeout=30000
原文:https://www.cnblogs.com/xiaoruirui/p/13660618.html