feat:实现异步缓存支持

This commit is contained in:
wangyu 2021-12-02 20:46:12 +08:00
parent 3fd039de21
commit b7fdbba498
16 changed files with 750 additions and 33 deletions

View File

@ -4,11 +4,11 @@ import com.flyfish.framework.constant.Frameworks;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import reactor.core.publisher.Mono;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
/**
@ -54,9 +54,16 @@ public class Result<T> {
return new Result<>(SUCCESS, MSG_SUCCESS, data);
}
public Result<T> end(Runnable runnable) {
runnable.run();
return this;
public static <T> Mono<Result<T>> just(T data) {
return Mono.just(accept(data));
}
public static <T> Mono<Result<List<T>>> just(Page<T> page) {
return Mono.just(accept(page));
}
public static <T> Mono<Result<T>> just() {
return Mono.just(ok());
}
/**
@ -115,6 +122,11 @@ public class Result<T> {
return new Result<>(FAIL, errMsg, null);
}
public Result<T> end(Runnable runnable) {
runnable.run();
return this;
}
public Result<Void> toVoid() {
return (Result<Void>) this;
}
@ -157,14 +169,14 @@ public class Result<T> {
return Frameworks.config.isResultStyle() ? null : data;
}
public T getResult() {
return Frameworks.config.isResultStyle() ? data : null;
}
public void setData(T data) {
this.data = data;
}
public T getResult() {
return Frameworks.config.isResultStyle() ? data : null;
}
public String getCode() {
return code;
}
@ -177,11 +189,11 @@ public class Result<T> {
return Frameworks.config.isResultStyle() ? null : msg;
}
public String getMessage() {
return Frameworks.config.isResultStyle() ? msg : null;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMessage() {
return Frameworks.config.isResultStyle() ? msg : null;
}
}

View File

@ -14,7 +14,7 @@ import java.util.Optional;
public final class UserContext {
private static UserContext instance;
private ThreadLocal<User> userThreadLocal = new ThreadLocal<>();
private final ThreadLocal<User> userThreadLocal = new ThreadLocal<>();
public UserContext() {
instance = this;

View File

@ -1,6 +1,7 @@
package com.flyfish.framework.annotations;
import com.flyfish.framework.config.WebSecurityConfig;
import com.flyfish.framework.configuration.redis.EnableReactiveRedis;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
@ -8,6 +9,7 @@ import java.lang.annotation.*;
/**
* 启用自动security配置
*
* @author wangyu
*/
@Retention(RetentionPolicy.RUNTIME)
@ -15,5 +17,6 @@ import java.lang.annotation.*;
@Documented
@Import(WebSecurityConfig.class)
@EnableReactiveMongoRepositories(basePackages = "com.flyfish.framework")
@EnableReactiveRedis
public @interface EnableAutoSecurity {
}

View File

@ -0,0 +1,25 @@
package com.flyfish.framework.domain.authorized;
import com.flyfish.framework.domain.base.AuditDomain;
import lombok.Getter;
import lombok.Setter;
/**
* 带鉴权的实体主要以部门隔绝
* @param <T>
*/
@Getter
@Setter
public abstract class AuthorizedDomain<T extends AuthorizedDomain<T>> extends AuditDomain {
// 作用域id一般是部门用户存储时插入
private String authorizeId;
public String getAuthorizeId() {
return
}
public String setAuthorizeId() {
}
}

View File

@ -0,0 +1,53 @@
package com.flyfish.framework.domain.authorized;
import com.flyfish.framework.builder.CriteriaBuilder;
import com.flyfish.framework.context.SpringContext;
import com.flyfish.framework.domain.base.NameLikeQo;
import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.service.DepartmentService;
import lombok.Getter;
import lombok.Setter;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 带鉴权的查询实体主要以部门隔绝
*
* @param <T>
*/
@Getter
@Setter
public abstract class AuthorizedQo<T extends AuthorizedDomain<T>> extends NameLikeQo<T> {
// 部门服务
private DepartmentService departmentService;
/**
* 获取可见的权限ids
*
* @return 结果
*/
public List<String> getAuthorizedIds() {
return Optional.ofNullable(this.user)
.map(User::getDepartments)
.map(departs -> departs.stream().map(Department::getId).collect(Collectors.toList()))
.map(this::getSubAuthorities)
.orElse(Collections.emptyList());
}
@Override
public CriteriaBuilder<T> criteriaBuilder() {
return super.criteriaBuilder().with("authorizedIds", "authorizeId", CriteriaBuilder.Builders.IN);
}
private List<String> getSubAuthorities(List<String> parents) {
if (null == departmentService) {
departmentService = SpringContext.getBean(DepartmentService.class);
}
return departmentService.getSubCodes(parents);
}
}

View File

@ -2,9 +2,35 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.service.impl.BaseServiceImpl;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
* 部门服务
*
* @author wangyu
*/
@Service
public class DepartmentService extends BaseServiceImpl<Department> {
@Resource
private MongoOperations mongoOperations;
/**
* 获取子部门列表
*
* @return 结果
*/
public List<String> getSubCodes(List<String> parents) {
Query query = Query.query(Criteria.where("parentId").in(parents));
query.fields().include("_id");
return mongoOperations.find(query, Department.class).stream().map(Department::getId)
.collect(Collectors.toList());
}
}

View File

@ -3,9 +3,14 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.repository.ReactiveUserRepository;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import com.flyfish.framework.utils.RedisOperations;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
/**
* 异步用户service
*
@ -14,14 +19,25 @@ import reactor.core.publisher.Mono;
@Service
public class ReactiveUserService extends BaseReactiveServiceImpl<User> {
@Resource
private RedisOperations redisOperations;
/**
* 获取用户数据
*
* @param username 用户
* @return 结果
*/
@Cacheable
public Mono<User> findByUsername(String username) {
if (redisOperations.hasKey(getCacheKey(username))) {
return
}
return ((ReactiveUserRepository) repository).findByUsername(username);
}
private String getCacheKey(String username) {
return "user-" + username;
}
}

View File

@ -13,4 +13,7 @@ import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestContextBody {
boolean required() default true;
}

View File

@ -0,0 +1,17 @@
package com.flyfish.framework.configuration.redis;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 启用自动security配置
*
* @author wangyu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(ReactiveRedisConfig.class)
public @interface EnableReactiveRedis {
}

View File

@ -0,0 +1,17 @@
package com.flyfish.framework.configuration.redis;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import java.lang.annotation.*;
/**
* 启用自动security配置
* @author wangyu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(RedisConfig.class)
public @interface EnableRedis {
}

View File

@ -0,0 +1,19 @@
package com.flyfish.framework.configuration.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
public class JacksonRedisSerializerFactory {
public static Jackson2JsonRedisSerializer<Object> produce() {
// 设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
return jackson2JsonRedisSerializer;
}
}

View File

@ -0,0 +1,51 @@
package com.flyfish.framework.configuration.redis;
import com.flyfish.framework.utils.ReactiveRedisOperations;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
public class ReactiveRedisConfig {
@Bean
public ReactiveStringRedisTemplate reactiveStringRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
return new ReactiveStringRedisTemplate(lettuceConnectionFactory);
}
/**
* RedisTemplate配置
*
* @param lettuceConnectionFactory 支持异步的连接池
* @return 结果
*/
@Bean
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
Jackson2JsonRedisSerializer<Object> jacksonSerializer = JacksonRedisSerializerFactory.produce();
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
.<String, Object>newSerializationContext()
// key序列化
.key(stringSerializer)
// value序列化
.value(jacksonSerializer)
// Hash key序列化
.hashKey(stringSerializer)
// Hash value序列化
.hashValue(jacksonSerializer)
.build();
// 配置redisTemplate
return new ReactiveRedisTemplate<>(lettuceConnectionFactory, serializationContext);
}
@Bean
public ReactiveRedisOperations redisOperations(ReactiveStringRedisTemplate reactiveStringRedisTemplate,
ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
return new ReactiveRedisOperations(reactiveRedisTemplate, reactiveStringRedisTemplate);
}
}

View File

@ -1,11 +1,7 @@
package com.flyfish.framework.configuration;
package com.flyfish.framework.configuration.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.flyfish.framework.utils.RedisOperations;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@ -13,7 +9,6 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@ -24,32 +19,28 @@ public class RedisConfig {
/**
* RedisTemplate配置
*
* @param lettuceConnectionFactory
* @return
* @param lettuceConnectionFactory 连接池
* @return 结果
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
Jackson2JsonRedisSerializer<Object> jacksonSerializer = JacksonRedisSerializerFactory.produce();
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);// key序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
redisTemplate.setValueSerializer(jacksonSerializer);// value序列化
redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
redisTemplate.setHashValueSerializer(jacksonSerializer);// Hash value序列化
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean
public RedisOperations redisOperations(LettuceConnectionFactory lettuceConnectionFactory, StringRedisTemplate stringRedisTemplate) {
return new RedisOperations(redisTemplate(lettuceConnectionFactory), stringRedisTemplate);
public RedisOperations redisOperations(RedisTemplate<String, Object> redisTemplate, StringRedisTemplate stringRedisTemplate) {
return new RedisOperations(redisTemplate, stringRedisTemplate);
}
}

View File

@ -54,8 +54,7 @@ public class PageQueryArgumentResolver extends HandlerMethodArgumentResolverSupp
// 使用spring的hack来解析pageable
Pageable pageable = pageableParameterFilter.filter(parameter, bindingContext, exchange);
// 使用spring的默认解析器解析所有变量
return modelAttributeFilter.filter(parameter,
bindingContext, exchange).flatMap(qo -> {
return modelAttributeFilter.filter(parameter, bindingContext, exchange).flatMap(qo -> {
if (qo instanceof Qo) {
Qo query = (Qo) qo;
query.setPageable(pageable);

View File

@ -37,7 +37,7 @@ public class RequestContextBodyArgumentResolver extends AbstractMessageReaderArg
@Override
public Mono<Object> resolveArgument(
MethodParameter param, BindingContext bindingContext, ServerWebExchange exchange) {
RequestBody ann = param.getParameterAnnotation(RequestBody.class);
RequestContextBody ann = param.getParameterAnnotation(RequestContextBody.class);
Assert.state(ann != null, "No RequestBody annotation");
return readBody(param, ann.required(), bindingContext, exchange)
.flatMap(object -> {

View File

@ -0,0 +1,485 @@
package com.flyfish.framework.utils;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.util.CastUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
/**
* redis 工具类
*
* @author wangyu
*/
@RequiredArgsConstructor
public class ReactiveRedisOperations {
private final ReactiveRedisTemplate<String, Object> redisTemplate;
private final ReactiveStringRedisTemplate stringRedisTemplate;
/**
* 指定缓存失效时间
*
* @param key
* @param time 时间()
* @return 是否成功指定
*/
public Mono<Boolean> expire(String key, long time) {
return redisTemplate.expire(key, Duration.of(time, ChronoUnit.SECONDS))
.doOnError(Throwable::printStackTrace)
.onErrorReturn(false);
}
/**
* 根据key 获取过期时间
*
* @param key 不能为null
* @return 时间() 返回0代表为永久有效
*/
public Mono<Long> getExpire(String key) {
return redisTemplate.getExpire(key).map(Duration::getSeconds);
}
/**
* 判断key是否存在
*
* @param key
* @return true 存在 false不存在
*/
public Mono<Boolean> hasKey(String key) {
return redisTemplate.hasKey(key).doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
public Mono<Long> del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
return redisTemplate.delete(key[0]);
} else {
return redisTemplate.delete(key);
}
}
return Mono.just(0L);
}
// ============================String=============================
/**
* 普通缓存获取
*
* @param key
* @return
*/
public <T> Mono<T> get(String key) {
if (key == null) {
return null;
}
try {
return redisTemplate.opsForValue().get(key).map(CastUtils::cast);
} catch (Exception e) {
return stringRedisTemplate.opsForValue().get(key).map(CastUtils::cast);
}
}
/**
* 获取string
*
* @param key
* @return 结果
*/
public Mono<String> getString(String key) {
return null == key ? Mono.empty() : stringRedisTemplate.opsForValue().get(key);
}
/**
* 获取多个key值
*
* @param key
* @return 结果
*/
public Flux<String> getKeys(String key) {
return null == key ? Flux.empty() : redisTemplate.keys(key);
}
/**
* 普通缓存放入
*
* @param key
* @param value
* @return true成功 false失败
*/
public Mono<Boolean> set(String key, Object value) {
return redisTemplate.opsForValue().set(key, value)
.doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
public Mono<Boolean> set(String key, String value) {
return stringRedisTemplate.opsForValue().set(key, value)
.doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
/**
* 普通缓存放入并设置时间
*
* @param key
* @param value
* @param time 时间() time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public Mono<Boolean> set(String key, Object value, long time) {
if (time > 0) {
return redisTemplate.opsForValue().set(key, value, Duration.of(time, ChronoUnit.SECONDS))
.doOnError(Throwable::printStackTrace).onErrorReturn(false);
} else {
return set(key, value);
}
}
/**
* 递增
*
* @param key
* @param delta 要增加几(大于0)
* @return 结果
*/
public Mono<Long> incr(String key, long delta) {
if (delta < 0) {
return Mono.error(new RuntimeException("递增因子必须大于0"));
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key
* @param delta 要减少几(小于0)
* @return
*/
public Mono<Long> decr(String key, long delta) {
if (delta < 0) {
return Mono.error(new RuntimeException("递减因子必须大于0"));
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Map=================================
/**
* HashGet
*
* @param key 不能为null
* @param item 不能为null
* @return
*/
public Mono<Object> hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key
* @return 对应的多个键值
*/
public Flux<Map.Entry<Object, Object>> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key
* @param map 对应多个键值
* @return true 成功 false 失败
*/
public Mono<Boolean> hmset(String key, Map<String, Object> map) {
return redisTemplate.opsForHash().putAll(key, map).doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
/**
* HashSet 并设置时间
*
* @param key
* @param map 对应多个键值
* @param time 时间()
* @return true成功 false失败
*/
public Mono<Boolean> hmset(String key, Map<String, Object> map, long time) {
return redisTemplate.opsForHash().putAll(key, map).doOnError(Throwable::printStackTrace)
.flatMap(e -> time > 0 ? expire(key, time).thenReturn(true) : Mono.just(true))
.onErrorReturn(false);
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @return true 成功 false失败
*/
public Mono<Boolean> hset(String key, String item, Object value) {
return redisTemplate.opsForHash().put(key, item, value).doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key
* @param item
* @param value
* @param time 时间() 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
public Mono<Boolean> hset(String key, String item, Object value, long time) {
return redisTemplate.opsForHash().put(key, item, value)
.doOnError(Throwable::printStackTrace)
.flatMap(e -> time > 0 ? expire(key, time).thenReturn(true) : Mono.just(true))
.onErrorReturn(false);
}
/**
* 删除hash表中的值
*
* @param key 不能为null
*/
public Mono<Boolean> hdel(String key) {
return redisTemplate.opsForHash().delete(key);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 不能为null
* @param item 不能为null
* @return true 存在 false不存在
*/
public Mono<Boolean> hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key
* @param item
* @param by 要增加几(大于0)
* @return
*/
public Mono<Double> hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key
* @param item
* @param by 要减少记(小于0)
* @return
*/
public Mono<Double> hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================set=============================
/**
* 根据key获取Set中的所有值
*
* @param key
* @return 所有值
*/
public Flux<Object> sGet(String key) {
return redisTemplate.opsForSet().members(key).doOnError(Throwable::printStackTrace)
.onErrorResume(e -> Flux.empty());
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key
* @param value
* @return true 存在 false不存在
*/
public Mono<Boolean> sHasKey(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value).doOnError(Throwable::printStackTrace).onErrorReturn(false);
}
/**
* 将数据放入set缓存
*
* @param key
* @param values 可以是多个
* @return 成功个数
*/
public Mono<Long> sSet(String key, Object... values) {
return redisTemplate.opsForSet().add(key, values).doOnError(Throwable::printStackTrace).onErrorReturn(0L);
}
/**
* 将set数据放入缓存
*
* @param key
* @param time 时间()
* @param values 可以是多个
* @return 成功个数
*/
public Mono<Long> sSetAndTime(String key, long time, Object... values) {
return redisTemplate.opsForSet().add(key, values).doOnError(Throwable::printStackTrace)
.flatMap(count -> time > 0 ? expire(key, time).thenReturn(count) : Mono.just(count))
.onErrorReturn(0L);
}
/**
* 获取set缓存的长度
*
* @param key
* @return
*/
public Mono<Long> sGetSetSize(String key) {
return redisTemplate.opsForSet().size(key).doOnError(Throwable::printStackTrace).onErrorReturn(0L);
}
/**
* 移除值为value的
*
* @param key
* @param values 可以是多个
* @return 移除的个数
*/
public Mono<Long> setRemove(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values).doOnError(Throwable::printStackTrace).onErrorReturn(0L);
}
// ===============================list=================================
/**
* 获取list缓存的内容
*
* @param key
* @param start 开始
* @param end 结束 0 -1代表所有值
* @return
*/
public Flux<Object> lGet(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end).doOnError(Throwable::printStackTrace)
.onErrorResume(e -> Flux.empty());
}
/**
* 获取list缓存的长度
*
* @param key
* @return
*/
public Mono<Long> lGetListSize(String key) {
return redisTemplate.opsForList().size(key).doOnError(Throwable::printStackTrace).onErrorReturn(0L);
}
/**
* 通过索引 获取list中的值
*
* @param key
* @param index 索引 index>=0时 0 表头1 第二个元素依次类推index<0时-1表尾-2倒数第二个元素依次类推
* @return
*/
public Mono<Object> lGetIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index).doOnError(Throwable::printStackTrace)
.onErrorResume(e -> Mono.empty());
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public Mono<Boolean> lSet(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value).doOnError(Throwable::printStackTrace)
.map(r -> true)
.onErrorReturn(false);
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public Mono<Boolean> lSet(String key, Object value, long time) {
return redisTemplate.opsForList().rightPush(key, value)
.doOnError(Throwable::printStackTrace)
.flatMap(e -> time > 0 ? expire(key, time).thenReturn(true) : Mono.just(true))
.onErrorReturn(false);
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @return
*/
public Mono<Boolean> lSet(String key, List<Object> value) {
return redisTemplate.opsForList().rightPushAll(key, value)
.thenReturn(true)
.doOnError(Throwable::printStackTrace)
.onErrorReturn(false);
}
/**
* 将list放入缓存
*
* @param key
* @param value
* @param time 时间()
* @return
*/
public Mono<Boolean> lSet(String key, List<Object> value, long time) {
return redisTemplate.opsForList().rightPushAll(key, value)
.doOnError(Throwable::printStackTrace)
.flatMap(e -> time > 0 ? expire(key, time).thenReturn(true) : Mono.just(true))
.onErrorReturn(false);
}
/**
* 根据索引修改list中的某条数据
*
* @param key
* @param index 索引
* @param value
* @return
*/
public Mono<Boolean> lUpdateIndex(String key, long index, Object value) {
return redisTemplate.opsForList().set(key, index, value).doOnError(Throwable::printStackTrace)
.onErrorReturn(false);
}
/**
* 移除N个值为value
*
* @param key
* @param count 移除多少个
* @param value
* @return 移除的个数
*/
public Mono<Long> lRemove(String key, long count, Object value) {
return redisTemplate.opsForList().remove(key, count, value).doOnError(Throwable::printStackTrace)
.onErrorReturn(0L);
}
}