提交 6c745d79 编写于 作者: 老丢丢's avatar 老丢丢

更新代码

上级 37b2e923
1、数据库连接池是什么?
数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;
释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。
2、常见数据库连接池:
HikariCP:https://github.com/brettwooldridge/HikariCP
Tomcat pooling:http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
Commons DBCP2:http://commons.apache.org/proper/commons-dbcp/
Druid:https://github.com/alibaba/druid/
......@@ -35,9 +35,10 @@
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<!--<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.pannk.demo;
import com.pannk.demo.config.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;
import java.util.List;
/**
* Created by wolf on 20-11-13.
*/
@Slf4j
@SpringBootApplication
public class App implements CommandLineRunner{
public class App implements CommandLineRunner {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private RedisTemplate<String,Object> redisTemplate;
private RedisUtil redisUtil;
public static void main(String[] args) {
SpringApplication.run(App.class,args);
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) throws Exception {
/*redisTemplate.opsForValue().set("hello","Hello world redis");
String hello = (String) redisTemplate.opsForValue().get("hello");
List<RedisClientInfo> clientList = redisTemplate.getClientList();
clientList.stream().forEach(info->{
log.error("info:{}",info);
});
log.error("=========hello:{}",hello);
*/
for (int i = 0; i < 1000; i++) {
new Thread(() -> redisTemplate.opsForValue().set("key"+Thread.currentThread().getName(),"The value is "+Thread.currentThread().getId())).start();
}
public void run(String... args) {
/*for (int i = 0; i < 1000; i++) {
new Thread(() -> {
String key = Thread.currentThread().getName();
redisTemplate.opsForValue().set(key, String.valueOf(Thread.currentThread().getId()));
System.out.println("键值:" + key + " = " + redisTemplate.opsForValue().get(key));
}).start();
}*/
redisUtil.set("hello","okkk");
}
}
......@@ -7,16 +7,22 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Created by wolf on 20-11-13.
* Redis配置
*
* @author wolf
* @date 20-11-13
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory){
public RedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
//设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//设置键序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置值序列化
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
......
package com.pannk.demo.config;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Created by wolf on 20-11-3.
*/
@Slf4j
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 设置过期时间
* @param key 键
* @param time 时间,单位:秒
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("设置失效时间出错,{}", e);
return false;
}
}
/**
* 设置过期时间
* @param key 键
* @param day 时间,单位:天
* @return
*/
public boolean expire(String key,int day){
try{
if (day<0){
redisTemplate.expire(key,day,TimeUnit.DAYS);
}
return true;
}catch (Exception e){
log.error("设置失效时间出错,{}",e);
return false;
}
}
/**
* 删除键
* @param key 键
*/
public void del(String... key){
if (key!=null && key.length>0){
if (key.length==1){
redisTemplate.delete(key[0]);
}else{
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
/**
* 缓存获取
* @param key 键
* @return
*/
public Object get(String key){
return key == null ? null :redisTemplate.opsForValue().get(key);
}
/**
* 获取对象
* @param key 键
* @param cls 类
* @param <T> 类型
* @return
*/
public <T> T getEntity(String key,Class<T> cls){
Object obj = this.get(key);
if (obj!=null){
return JSON.parseObject(obj.toString(),cls);
}
return null;
}
/**
* 获取列表
* @param key 键
* @param cls 类
* @param <T> 类型
* @return
*/
public <T> List<T> getListEntity(String key, Class<T> cls){
Object obj = this.get(key);
if (obj!=null){
return JSON.parseArray(obj.toString(),cls);
}
return null;
}
/**
* 设置键值
* @param key 键
* @param value 值
* @return
*/
public boolean set(String key,Object value){
try{
redisTemplate.opsForValue().set(key,value);
return true;
}catch (Exception e){
log.error("设置键值出错,{}",e);
return false;
}
}
/**
* 设置键值和失效时间
* @param key 键
* @param value 值
* @param time 失效时间
* @return
*/
public boolean set(String key,Object value,long time){
try{
if(time>0){
redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
}else{
set(key,value);
}
return true;
}catch (Exception e){
log.error("设置键值和失效时间出错,{}",e);
return false;
}
}
/**
* 获取hash值
* @param key 键
* @param item 项
* @return
*/
public Object hget(String key,String item){
return redisTemplate.opsForHash().get(key,item);
}
/**
* 获取hashKey对应的所有键值
* @param key 键
* @return
*/
public Map<Object,Object> hmget(String key){
return redisTemplate.opsForHash().entries(key);
}
/**
* 设置hash值
* @param key 键
* @param map 值
* @return
*/
public boolean hmset(String key,Map<String,Object> map){
try{
redisTemplate.opsForHash().putAll(key,map);
return true;
}catch (Exception e){
log.error("设置哈希键值出错,{}",e);
return false;
}
}
}
......@@ -8,7 +8,7 @@ spring:
database: 0
jedis:
pool:
max-idle: 10
max-idle: 100
min-idle: 10
max-wait: -1ms
max-active: 100
1、Redis是什么?
Redis是现在最受欢迎的NoSQL数据库之一,Redis是一个使用ANSI C编写的开源、包含多种数据结构、支持网络、基于内存、可选持久性的键值对存储数据库,
其具备如下特性:
基于内存运行,性能高效
支持分布式,理论上可以无限扩展
key-value存储系统
开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API
2、应用场景有哪些?
缓存系统(“热点”数据:高频读、低频写)、计数器、消息队列系统、排行榜、社交网络和实时系统。
3、有哪些数据类型?
Redis提供的数据类型主要分为5种自有类型和一种自定义类型,这5种自有类型包括:String类型、哈希类型、列表类型、集合类型和顺序集合类型。
4、常用客户端有哪些?
Lettuce:https://lettuce.io/
Jedis:http://tool.oschina.net/uploads/apidocs/redis/clients/jedis/Jedis.html
redisson:https://redisson.org/
概念:
Jedis:是Redis的Java实现客户端,提供了比较全面的Redis命令的支持,
Redisson:实现了分布式和可扩展的Java数据结构。
Lettuce:高级Redis客户端,用于线程安全同步,异步和响应使用,支持集群,Sentinel,管道和编码器。
优点:
Jedis:比较全面的提供了Redis的操作特性
Redisson:促使使用者对Redis的关注分离,提供很多分布式相关操作服务,例如,分布式锁,分布式集合,可通过Redis支持延迟队列
Lettuce:主要在一些分布式缓存框架上使用比较多(SpringBoot2默认使用的客户端)
可伸缩:
Jedis:使用阻塞的I/O,且其方法调用都是同步的,程序流需要等到sockets处理完I/O才能执行,不支持异步。Jedis客户端实例不是线程安全的,
所以需要通过连接池来使用Jedis。
Redisson:基于Netty框架的事件驱动的通信层,其方法调用是异步的。Redisson的API是线程安全的,所以可以操作单个Redisson连接来完成各种操作
Lettuce:基于Netty框架的事件驱动的通信层,其方法调用是异步的。Lettuce的API是线程安全的,所以可以操作单个Lettuce连接来完成各种操作
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册