Skip to content

Commit

Permalink
finished all
Browse files Browse the repository at this point in the history
  • Loading branch information
upangka committed Dec 22, 2023
1 parent 7814df7 commit 9193980
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/Redis/01 安装Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ firewall-cmd --reload
> redis-server 指定不同的配置文件,可以在一台机器上启动多个实例
```sh
cd /usr/redis/redis-6.2.6
cd /usr/local/redis/redis-6.2.6
# 启动服务
src/redis-server redis.conf
```
Expand Down
72 changes: 72 additions & 0 deletions docs/java/58 空字符串.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
sidebarDepth: 3
sidebar: auto
prev:
text: Back To 目录
link: /java/
typora-root-url: ..\.vuepress\public
---



unicode为12288字符为全角空格,trim()无法去除,去除方法如下:

```java
str = str.replace((char) 12288, ' ');
str=str.trim();
```



```java
public class StringUtils {

public static void main(String[] args) {
String str = " ";
printStrAscii(str);
char space = 12288;
String strTwo = "" + space;
printStrAscii(strTwo);

str = str.trim();
strTwo = strTwo.trim();
System.out.println(str.length());
System.out.println(strTwo.length());
}

private static void printStrAscii(String str) {
System.out.print(str + " : ");
for(int i=0; i<str.length(); i++) {
System.out.print((int)str.charAt(i));
}
System.out.println();
}
}
/**
* : 32
*   : 12288
* 0
* 1
*/
```

> 判断为是否空字符串
```java
/**
* 判断是否为null或空字符
*/
public static boolean isNullOrEmpty(Object o) {
if (o == null) {
return true;
}
if (String.valueOf(o).replace((char) 12288, ' ').trim().length() == 0) {
return true;
}
if ("null".equals(o)) {
return true;
}
return false;
}
```

18 changes: 17 additions & 1 deletion docs/springboot/15 openapi文档工具.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,25 @@ typora-root-url: ..\.vuepress\public

![image-20230603115651236](/images/springboot/image-20230603115651236.png)

引入依赖在Controller上配置相关信息就可以访问了`http://localhost:8082/swagger-ui/index.html`,没有配置context-path,默认就是`/`

```java
@RestController
@RequestMapping("/account")
@Tag(name = "管理界面用户管理",description = "用户管理登录等等")
@Slf4j
public class AccountController extends BaseController{
@Operation(summary = "保存用户信息", description = "保存用户信息到数据库")
@PostMapping("/save")
public void save() {
throw new NotImplementedException("接口未实现");
}
}
```



## 额外信息配置
## 额外信息配置(非强制)

![image-20230522135222296](/images/springboot/image-20230522135222296.png)

Expand Down
170 changes: 170 additions & 0 deletions docs/springboot/24 springboot cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
---
sidebarDepth: 3
sidebar: auto
prev:
text: Back To 目录
link: /springboot/
typora-root-url: ..\.vuepress\public
---

## spring

在spring中就已经引入了本地缓存,spring-context中就有了`CacheEvict`等注解

在springboot中,自动配置类中就配置了ConcurrentMapCacheManager,默认的。

下面这个依赖,方便引入其他缓存,如RedisCache,Ehcache等

```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
```



## 简单使用

Spring 从 3.1 开始就引入了对 Cache 的支持。定义了 `org.springframework.cache.Cache``org.springframework.cache.CacheManager` 接口来统一不同的缓存技术。并支持使用 `JCache(JSR-107)`注解简化我们的开发。

其使用方法和原理都类似于 Spring 对事务管理的支持。Spring Cache 是作用在方法上的,其核心思想是,当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存在缓存中。

```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
```

引入了springboot,就可以使用缓存了

1. 开启基于注解的缓存,使用 `@EnableCaching` 标识在 SpringBoot 的主启动类上。

2. 标注缓存注解即可

```java
@SpringBootApplication
@EnableCaching // 开启基于注解的缓存
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
```

```java
@Component
@Log
public class SimpleRunner implements CommandLineRunner, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void run(String... args) throws Exception {
log.info("-----------msg-------------------");
/**
* 因为spring的缓存是通过代理实现的,类似aop.被Spring包装了。
* 在该类方法直接执行,不会走代理,所以我们需要拿到代理类
*/
SimpleRunner simpleRunner = applicationContext.getBean(SimpleRunner.class);

log.info(simpleRunner.getData(1));
log.info(simpleRunner.getData(1));
log.info(simpleRunner.getData(1));
}

private AtomicInteger count = new AtomicInteger(0);
@Cacheable(cacheNames = "emps")
public String getData(Integer id){
return "msg"+count.incrementAndGet();
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
```

![image-20231222191923432](/images/springboot/image-20231222191923432.png)

### 常用属性说明



#### 常用属性说明

下面介绍一下 `@Cacheable `这个注解常用的几个属性:

> - `cacheNames/value` :用来指定缓存组件的名字
> - `key` :缓存数据时使用的 key,可以用它来指定。默认是使用方法参数的值。(这个 key 你可以使用 spEL 表达式来编写)
> - `keyGenerator` :key 的生成器。 key 和 keyGenerator 二选一使用
> - `cacheManager` :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。
> - `condition` :可以用来指定符合条件的情况下才缓存
> - `unless` :否定缓存。当 unless 指定的条件为 true ,方法的返回值就不会被缓存。当然你也可以获取到结果进行判断。(通过 `#result` 获取方法结果)
> - `sync` :是否使用异步模式。
#### spEL 编写 key

前面说过,缓存的 key 支持使用 spEL 表达式去编写,下面总结一下使用 spEL 去编写 key 可以用的一些元数据:

![img](/images/springboot/0b94988b3cde463ed16ca1edec244c1e.png)

## 引入第三方缓存Redis

springboot自动配置类配置了`RedisCacheConfiguration`,引入下面的依赖,就会注册`RedisCacheManager`

![image-20231222193138072](/images/springboot/image-20231222193138072.png)

```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```

![image-20231222194901593](/images/springboot/image-20231222194901593.png)

由于出现乱码,我们需要配置一下,首先需要引入jackson

```xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
```

```java
@Configuration
public class RedisConfig {
@Bean
@Primary
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(redisCacheConfiguration)
.build();
}
}
```

![image-20231222201249988](/images/springboot/image-20231222201249988.png)

```java
@Cacheable(cacheNames = "msg",key = "#root.targetClass.simpleName+':'+#id")
public String getData(Integer id){
return "msg"+count.incrementAndGet();
}
```



## 参考

[SpringBoot 缓存之 @Cacheable 详细介绍](https://xie.infoq.cn/article/001e0f5ab65fa7dd1484c51e5)

[Caching Data with Spring](https://spring.io/guides/gs/caching/)

[“8个步骤”手把手带你用SpringBoot操作Redis,实现数据缓存](https://cloud.tencent.com/developer/article/1824707)

0 comments on commit 9193980

Please sign in to comment.