Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
老丢丢
SpringBoot2Demo
提交
6c745d79
S
SpringBoot2Demo
项目概览
老丢丢
/
SpringBoot2Demo
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot2Demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6c745d79
编写于
11月 13, 2020
作者:
老丢丢
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
更新代码
上级
37b2e923
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
252 addition
and
26 deletion
+252
-26
integrate-druid/src/main/resources/note
integrate-druid/src/main/resources/note
+8
-0
integrate-redis/pom.xml
integrate-redis/pom.xml
+5
-4
integrate-redis/src/main/java/com/pannk/demo/App.java
integrate-redis/src/main/java/com/pannk/demo/App.java
+17
-19
integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java
...edis/src/main/java/com/pannk/demo/config/RedisConfig.java
+8
-2
integrate-redis/src/main/java/com/pannk/demo/config/RedisUtil.java
...-redis/src/main/java/com/pannk/demo/config/RedisUtil.java
+184
-0
integrate-redis/src/main/resources/application.yml
integrate-redis/src/main/resources/application.yml
+1
-1
integrate-redis/src/main/resources/note
integrate-redis/src/main/resources/note
+29
-0
未找到文件。
integrate-druid/src/main/resources/note
0 → 100644
浏览文件 @
6c745d79
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/
integrate-redis/pom.xml
浏览文件 @
6c745d79
...
...
@@ -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
integrate-redis/src/main/java/com/pannk/demo/App.java
浏览文件 @
6c745d79
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
Redis
Template
<
String
,
Object
>
redisTemplate
;
private
Redis
Util
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"
);
}
}
integrate-redis/src/main/java/com/pannk/demo/config/RedisConfig.java
浏览文件 @
6c745d79
...
...
@@ -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
;
}
...
...
integrate-redis/src/main/java/com/pannk/demo/config/RedisUtil.java
0 → 100644
浏览文件 @
6c745d79
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
;
}
}
}
integrate-redis/src/main/resources/application.yml
浏览文件 @
6c745d79
...
...
@@ -8,7 +8,7 @@ spring:
database
:
0
jedis
:
pool
:
max-idle
:
10
max-idle
:
10
0
min-idle
:
10
max-wait
:
-1ms
max-active
:
100
integrate-redis/src/main/resources/note
0 → 100644
浏览文件 @
6c745d79
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录