安装
参考:http://www.redis.cn/download.html
安装
下载,解压,编译:
$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar -zcvf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ make
启动
src/redis-server
内置的命令行工具command line interface
src/redis-cli
参考:http://www.cnblogs.com/liuling/p/2014-4-19-04.html
依赖
jedis:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
连接池:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
jedis操作redis
package impl.service.redis;
import face.service.RedisService;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author futao
* Created on 18-2-28-下午5:15.
*/
public class RedisServiceImpl implements RedisService {
private Jedis jedis;
@Before
public void setup() {
/*连接redis服务器*/
jedis = new Jedis("127.0.0.1", 6379);
/*权限,密码*/
//jedis.auth("");
}
/**
* jedis存储字符串
*/
@Test
public void testString() {
/*向redis中存String键值对 set*/
jedis.set("name", "futao");
/*从redis中读取String get*/
System.out.println(jedis.get("name"));
/*拼接*/
jedis.append("name", " is 666");
System.out.println(jedis.get("name"));
/*删除某个键值对*/
jedis.del("name");
System.out.println("del===" + jedis.get("name"));
/*一次性添加多个键值对*/
jedis.mset("k1", "v1", "k2", "v2", "age", "16");
System.out.println(jedis.get("k1"));
System.out.println(jedis.get("k2"));
System.out.println(jedis.get("age"));
/*age加一*/
jedis.incr("age");
System.out.println(jedis.get("age"));
/*String类型的数据设置过期时间*/
jedis.setex("name", 5, "Nicai");
System.out.println(jedis.get("name"));
}
/**
* jedis操作map
*/
@Test
public void testMap() {
Map<String, String> map = new HashMap<>();
map.put("user", "futao");
map.put("age", "18");
map.put("sex", "男");
/*存hashMap数据,hmset()*/
jedis.hmset("futaoInfo", map);
/*设置过期时间*/
jedis.expire("futaoInfo", 5);
/*读取hashMap数据结构的数据。返回的是一个List*/
System.out.println(jedis.hmget("futaoInfo", "sex", "user"));
List<String> list = jedis.hmget("futaoInfo", "sex", "sex", "age");
list.forEach(System.out::println);
/*删除map中的某个键值对*/
jedis.hdel("futaoInfo", "age");
/*返回key为futaoInfo的键中存放的值的个数*/
System.out.println("hlen===" + jedis.hlen("futaoInfo"));
/*是否存在该key*/
System.out.println("exist===" + jedis.exists("futaoInfo"));
/*返回map对象中的所有key */
System.out.println("map-all-keys" + jedis.hkeys("futaoInfo"));
/*返回map对象中的所有value */
System.out.println("map-all-value" + jedis.hvals("futaoInfo"));
Iterator<String> iterator = jedis.hkeys("futaoInfo").iterator();
/*遍历map中存储的key-value*/
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key + "::" + jedis.hget("futaoInfo", key));
}
for (String key : jedis.hkeys("futaoInfo")) {
System.out.println(key + "::" + jedis.hget("futaoInfo", key));
}
}
/**
* jedis操作list
*/
@Test
public void testList() {
/*添加list数据*/
jedis.lpush("javaFramework", "Spring", "SpringMVC", "Hibernate");
/*获取list中的数据.-1表示获取所有数据*/
List<String> javaFramework = jedis.lrange("javaFramework", 0, -1);
for (String a : javaFramework) {
System.out.println(a);
}
}
/**
* jedis操作set
* **********************
* * jedis支持有序set集合 *
* **********************
*/
@Test
public void testSet() {
/*存*/
jedis.sadd("username", "futao");
jedis.sadd("username", "futao1");
jedis.sadd("username", "futao2");
jedis.sadd("username", "futao3", "futao4", "futao5");
/*读取set的所有value*/
System.out.println(jedis.smembers("username"));
/*判断futao是否是username的value*/
System.out.println(jedis.sismember("username", "futao"));
/*从key为username的set中随机读取一个值*/
System.out.println(jedis.srandmember("username"));
/*返回集合元素的个数*/
System.out.println(jedis.scard("username"));
}
@Test
public void test() {
jedis.del("b");
jedis.rpush("b", "7");
jedis.lpush("b", "-5");
jedis.lpush("b", "3");
jedis.lpush("b", "99");
jedis.lpush("b", "-666");
System.out.print("before sort" + jedis.lrange("b", 0, -1));
List<String> b = jedis.sort("b");
System.out.println("B" + b);
/*并不会改变原来的数据*/
System.out.print("\nafter sort" + jedis.lrange("b", 0, -1));
}
}
redis连接池
package impl.service.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author futao
* Created on 18-2-28-下午7:13.
*/
public class RedisUtil {
/*服务器地址*/
private static final String HOST = "127.0.0.1";
/*端口*/
private static final int PORT = 6379;
/*权限密码*/
private static final String PASSWORD = null;
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static final int MAX_TOTAL = 1024;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static final int MAX_IDLE = 200;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static final int MAX_WAIT = 10000;
private static final int TIMEOUT = 10000;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static final boolean TEST_ON_BORROW = true;
private static JedisPool jedisPool = null;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(MAX_TOTAL);
jedisPoolConfig.setMaxIdle(MAX_IDLE);
jedisPoolConfig.setMaxWaitMillis(MAX_WAIT);
jedisPoolConfig.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(jedisPoolConfig, HOST, PORT, TIMEOUT, PASSWORD);
}
/**
* 获取jedis
*
* @return jedis实例
*/
public synchronized static Jedis getJedis() {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void releaseResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
@Test
public void testRedisPool() {
Jedis jedis = RedisUtil.getJedis();
jedis.del("a");
jedis.lpush("a", "111", "222", "333");
List<String> a = jedis.lrange("a", 0, -1);
for (String i : a) {
System.out.println(i);
}
/*记得要释放资源*/
RedisUtil.releaseResource(jedis);
}