nosql产品reids mongodb membase hbase
redis优势
- redis开源免费
- redis支持数据的持久化
- redis 支持丰富的数据类型,string,list,set,zset,hash
- redis支持数据的备份,主从,分片,集群
- 性能高 10W/s的读写,原子性操作,支持简单的队列,过期时间
安装redis
为了简单,用docker安装
docker run -d --name=redis7000 -p 7000:6379 redis
java 操作redis需要用到jedis客户端
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
常见操作
/**
* @author cailonghao
*/
public class JedisConfig {
/**
* 单机redis
*/
static Jedis jedis = new Jedis("192.168.116.150", 7001);
/**
* string
*/
public static void jedisString() {
jedis.set("cainiao", "xianfei");
System.out.println(jedis.get("cainiao"));
jedis.set(String.valueOf(1), "version:1");
System.out.println(jedis.mget("cainiao", "1"));
System.out.println(jedis.get("1"));
}
/**
* list
*/
public static void jedisList() {
jedis.rpush("list","1");
jedis.lpush("a","6");
jedis.lpush("a","7","8","9","10");
}
/**
* map
*/
public static void jedismap() {
Map<String, String> map = new HashMap<>();
map.put("name", "cainiao");
map.put("value", "hello");
jedis.hmset("user", map);
//第一个参数是key .后面是map的key .可以跟多个
List<String> lsmap = jedis.hmget("user", "name", "value");
System.out.println(lsmap);
}
/**
* set 无序,唯一
*/
public static void jedisset() {
jedis.sadd("fruit", "苹果");
jedis.sadd("fruit", "梨");
jedis.sadd("fruit", "榴莲");
jedis.sadd("fruit", "鱼");//添加
jedis.srem("fruit", "鱼");//移除
System.out.println(jedis.smembers("fruit"));
System.out.println(jedis.sismember("fruit", "鱼"));
System.out.println(jedis.srandmember("fruit"));
System.out.println(jedis.scard("fruit"));
}
public static void main(String[] args) {
//JedisConfig.jedisString();
//JedisConfig.jedismap();
jedisset();
}
}
客户端连接Redis使用的是TCP协议,直连的方式每次需要建立TCP连接,所以要引入连接池管理连接
springboot自动配置好了连接池管理,只需要配置文件配置,而且redistemplate类也封装了常见操作,可以不用自己写配置类
为了学习:也可以自定义redis的连接池,工具类,
自定义连接池,工具类(网上一大堆工具类)
引入springboot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
server:
port: 9000
redis: #自定义属性
host: 192.168.116.150
port: 7000
timeout: 3000
poolMaxTotal: 600 #最大连接数
poolMaxIdle: 300 #最大空闲数
poolMaxWait: 3000 # 最大连接等待
block: true
连接池
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisPool {
private String host;
private int port;
private int timeout;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;
private boolean block;
@Bean
public JedisPool jedisPool(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(this.getPoolMaxIdle());
jedisPoolConfig.setMaxWaitMillis(this.getPoolMaxWait());
jedisPoolConfig.setMaxTotal(this.getPoolMaxTotal());
jedisPoolConfig.setBlockWhenExhausted(this.isBlock());
return new JedisPool(jedisPoolConfig,this.getHost(),this.getPort(),this.getTimeout());
}
<!-- setter 和 getter-->
使用
@Controller
public class JedisController {
@Autowired
private JedisPool jedisPool;
public void test(){
Jedis jedis = jedisPool.getResource();
System.out.println(jedis.get("cainiao"));
System.out.println(jedisPool.isClosed());
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Test {
@Autowired
private JedisController jedisController;
@org.junit.Test
public void test1(){
jedisController.test();
}
}
写个简单的封装类
/**
* @author toms
* redis工具类
* */
public class RedisUtil {
@Autowired
private JedisPool jedisPool;
/**
* 获取实例
*/
public Jedis getJedis() {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
//todo
}
return jedis;
}
/**
* get 字符串
* */
public String get(String key) {
Jedis jedis = null;
String result = null;
try {
jedis = getJedis();
result = jedis.get(key);
}catch (Exception e){
//todo
}
jedis.close();
return result;
}
/**
* set 字符串
* */
public String set(String key, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = getJedis();
result = jedis.set(key,value);
}catch (Exception e){
//todo
}
jedis.close();
return result;
}
/**
* del 字符串
* */
public Long del(String...keys) {
Jedis jedis = null;
Long result = null;
try {
jedis = getJedis();
result = jedis.del(keys);
}catch (Exception e){
//todo
}
jedis.close();
return result;
}
/**
* 判断key是否存在
*
* @param key
* @return
*/
public Boolean exists(String key) {
Jedis jedis = null;
Boolean result = null;
try {
jedis = getJedis();
result = jedis.exists(key);
}catch (Exception e){
//todo
}
return result;
}
/**
* 设置key value,如果key已经存在则返回0
*/
public Long setnx(String key, String value) {
Jedis jedis = null;
Long result = null;
try {
jedis = getJedis();
result = jedis.setnx(key,value);
}catch (Exception e){
//todo
}
jedis.close();
return result;
}
/**
* 设置key value并指定这个键值的有效期
*
* @param key
* @param seconds
* @param value
* @return
*/
public String setex(String key, int seconds, String value) {
Jedis jedis = null;
String result = null;
try {
jedis = getJedis();
result = jedis.setex(key,seconds,value);
}catch (Exception e){
//todo
}
jedis.close();
return result;
}
//.................more
}
我们要把对象放进redis,就需要把对象转为json,或者拿出字符,转为对象,redistemplate同样帮我们封装好了,我们也可以自定义转化方式
引入json转换工具
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
写一个工具类