您的位置:首页 > 房产 > 建筑 > 2023新一轮病毒来袭_全网推广引流_专门看广告的网站_百度推广app下载官方

2023新一轮病毒来袭_全网推广引流_专门看广告的网站_百度推广app下载官方

2025/4/21 7:46:05 来源:https://blog.csdn.net/Zwwxd666/article/details/147235536  浏览:    关键词:2023新一轮病毒来袭_全网推广引流_专门看广告的网站_百度推广app下载官方
2023新一轮病毒来袭_全网推广引流_专门看广告的网站_百度推广app下载官方

application.yml

spring:application:name: demo#Redis相关配置redis:data:# 地址host: localhost# 端口,默认为6379port: 6379# 数据库索引database: 0# 密码password:# 连接超时时间timeout: 10slettuce:pool:# 连接池中的最小空闲连接min-idle: 0# 连接池中的最大空闲连接max-idle: 8# 连接池的最大数据库连接数max-active: 8# #连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1ms

依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>

序列化配置

package com.example.demo.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {//编写我们自己的配置redisTemplate@Bean@SuppressWarnings("all")public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// JSON序列化配置Jackson2JsonRedisSerializer jsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper=new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jsonRedisSerializer.setObjectMapper(objectMapper);// String的序列化StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();//key和hash的key都采用String的序列化方式template.setKeySerializer(stringRedisSerializer);template.setHashKeySerializer(stringRedisSerializer);//value和hash的value都采用jackson的序列化方式template.setValueSerializer(jsonRedisSerializer);template.setHashValueSerializer(jsonRedisSerializer);template.afterPropertiesSet();return template;}
}

工具类

package com.example.demo.utils;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;import java.util.*;
import java.util.concurrent.TimeUnit;/*** spring redis 工具类** @author ruoyi**/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisCache
{@Autowiredpublic RedisTemplate redisTemplate;/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值*/public <T> void setCacheObject(final String key, final T value){redisTemplate.opsForValue().set(key, value);}/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度*/public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit){redisTemplate.opsForValue().set(key, value, timeout, timeUnit);}/*** 设置有效时间** @param key Redis键* @param timeout 超时时间* @return true=设置成功;false=设置失败*/public boolean expire(final String key, final long timeout){return expire(key, timeout, TimeUnit.SECONDS);}/*** 设置有效时间** @param key Redis键* @param timeout 超时时间* @param unit 时间单位* @return true=设置成功;false=设置失败*/public boolean expire(final String key, final long timeout, final TimeUnit unit){return redisTemplate.expire(key, timeout, unit);}/*** 获取有效时间** @param key Redis键* @return 有效时间*/public long getExpire(final String key){return redisTemplate.getExpire(key);}/*** 判断 key是否存在** @param key 键* @return true 存在 false不存在*/public Boolean hasKey(String key){return redisTemplate.hasKey(key);}/*** 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public <T> T getCacheObject(final String key){ValueOperations<String, T> operation = redisTemplate.opsForValue();return operation.get(key);}/*** 删除单个对象** @param key*/public boolean deleteObject(final String key){return redisTemplate.delete(key);}/*** 删除集合对象** @param collection 多个对象* @return*/public boolean deleteObject(final Collection collection){return redisTemplate.delete(collection) > 0;}/*** 缓存List数据** @param key 缓存的键值* @param dataList 待缓存的List数据* @return 缓存的对象*/public <T> long setCacheList(final String key, final List<T> dataList){Long count = redisTemplate.opsForList().rightPushAll(key, dataList);return count == null ? 0 : count;}/*** 获得缓存的list对象** @param key 缓存的键值* @return 缓存键值对应的数据*/public <T> List<T> getCacheList(final String key){return redisTemplate.opsForList().range(key, 0, -1);}/*** 缓存Set** @param key 缓存键值* @param dataSet 缓存的数据* @return 缓存数据的对象*/public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet){BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);Iterator<T> it = dataSet.iterator();while (it.hasNext()){setOperation.add(it.next());}return setOperation;}/*** 获得缓存的set** @param key* @return*/public <T> Set<T> getCacheSet(final String key){return redisTemplate.opsForSet().members(key);}/*** 缓存Map** @param key* @param dataMap*/public <T> void setCacheMap(final String key, final Map<String, T> dataMap){if (dataMap != null) {redisTemplate.opsForHash().putAll(key, dataMap);}}/*** 获得缓存的Map** @param key* @return*/public <T> Map<String, T> getCacheMap(final String key){return redisTemplate.opsForHash().entries(key);}/*** 往Hash中存入数据** @param key Redis键* @param hKey Hash键* @param value 值*/public <T> void setCacheMapValue(final String key, final String hKey, final T value){redisTemplate.opsForHash().put(key, hKey, value);}/*** 获取Hash中的数据** @param key Redis键* @param hKey Hash键* @return Hash中的对象*/public <T> T getCacheMapValue(final String key, final String hKey){HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();return opsForHash.get(key, hKey);}/*** 获取多个Hash中的数据** @param key Redis键* @param hKeys Hash键集合* @return Hash对象集合*/public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys){return redisTemplate.opsForHash().multiGet(key, hKeys);}/*** 删除Hash中的某条数据** @param key Redis键* @param hKey Hash键* @return 是否成功*/public boolean deleteCacheMapValue(final String key, final String hKey){return redisTemplate.opsForHash().delete(key, hKey) > 0;}/*** 获得缓存的基本对象列表** @param pattern 字符串前缀* @return 对象列表*/public Collection<String> keys(final String pattern){return redisTemplate.keys(pattern);}
}

测试类

RedisCache

package com.example.demo.test;import com.example.demo.utils.RedisCache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;import java.util.*;
import java.util.concurrent.TimeUnit;@SpringBootTest
class RedisCacheTest {@AutowiredRedisCache redisCache;@AutowiredRedisTemplate redisTemplate;private static final String TEST_KEY = "test:list:key";// 1.缓存对象@Testpublic void setObject() {// redisCache.setCacheObject("object","test");// 附带过期时间redisCache.setCacheObject("object", "test", 10, TimeUnit.SECONDS);}// 2.过期时间@Testpublic void setTimeOut() {redisCache.setCacheObject("object", "test");// 更新过期时间redisCache.expire("object", 100, TimeUnit.SECONDS);// 获取过期时间System.out.println(redisCache.getExpire("object"));;}// 3.判断key是否存在@Testpublic void check() {System.out.println(redisCache.hasKey("object"));}// 4.获取对象@Testpublic void getObject() {System.out.println((String) redisCache.getCacheObject("object"));}// 5.删除对象/如果传递集合就是删除多个key@Testpublic void deleteObject() {redisCache.setCacheObject("obj", "1");System.out.println((String) redisCache.getCacheObject("obj"));redisCache.deleteObject("obj");System.out.println((String) redisCache.getCacheObject("obj"));}// 6.获取list对象@Testpublic void getList() {// 1. 准备测试数据List<String> testData = Arrays.asList("item1", "item2", "item3");redisCache.setCacheList(TEST_KEY, testData);// 2. 调用获取方法List<Object> result = redisCache.getCacheList(TEST_KEY);// 3. 验证结果for (Object o : result) {System.out.println(o);}}// 7.获取set对象@Testpublic void getSet() {// 1. 准备测试数据Set<String> set = Set.of("a", "b");redisCache.setCacheSet("TestSet", set);// 2. 调用获取方法Set<Object> cacheSet = redisCache.getCacheSet("TestSet");// 3. 验证结果for (Object o : cacheSet) {System.out.println(o);}}// 8.Map@Testpublic void getMap() {// 1. 准备测试数据Map<String, String> stringStringMap = new HashMap<>();stringStringMap.put("name","zww");stringStringMap.put("age","21");redisCache.setCacheMap("TestMap", stringStringMap);// 2. 调用获取方法Map<String, Object> testMap = redisCache.getCacheMap("TestMap");// 3. 验证结果System.out.println(testMap.get("name"));System.out.println(testMap.get("age"));}// 9.hash数据 对比map多了一个外层key@Testpublic void getHash() {// 1. 准备测试数据redisCache.setCacheMapValue("hashMap","testHashMap","1");redisCache.setCacheMapValue("hashMap","testHashMap2","2");// 2. 调用获取方法System.out.println((String) redisCache.getCacheMapValue("hashMap","testHashMap"));System.out.println((String) redisCache.getCacheMapValue("hashMap","testHashMap2"));}}

RedisTemplate

package com.example.demo.test;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;@SpringBootTest
public class RedisTemplateTest {@AutowiredRedisTemplate redisTemplate;/*** 操作String类型的数据*/@Testvoid contextLoads() {// redisTemplate.opsForValue().set("mannor" ,"rediaz");String mannor = (String) redisTemplate.opsForValue().get("mannor");System.out.println(mannor);redisTemplate.opsForValue().set("k1", "v1", 10L, TimeUnit.SECONDS);Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("mannor1", "mannor");System.out.println(aBoolean);}/*** 操作hash类型的数据*/@Testpublic void hashTest() {HashOperations hashOperations = redisTemplate.opsForHash();// 存hashOperations.put("002", "name", "zhangsan");hashOperations.put("002", "age", "20");hashOperations.put("002", "addr", "beijing");// 取Object age = hashOperations.get("002", "age");
//        System.out.println((String) age);// 获取所有字段Set keys = hashOperations.keys("002");for (Object key : keys) {
//            System.out.println(key);}// 获得hash结构中的所有值List values = hashOperations.values("002");for (Object value : values) {System.out.println(value);}}/*** 操作list类型的数据*/@Testpublic void listTest() {ListOperations listOperations = redisTemplate.opsForList();// 存listOperations.leftPush("list", "00");listOperations.leftPushAll("list", "01", "02", "03");// 取值List list = listOperations.range("list", 0, -1);for (Object val : list) {System.out.println(val);}System.out.println("------------------------------------------------------------");// 获取长度来遍历Long size = listOperations.size("list");for (int i = 0; i < size; i++) {// 出队列String element = (String) listOperations.rightPop("list");System.out.println(element);}}/*** 操作Set类型的数据*/@Testpublic void testSet() {SetOperations setOperations = redisTemplate.opsForSet();// 存值setOperations.add("myset", "a", "b", "c", "a");// 取值Set<String> myset = setOperations.members("myset");for (String o : myset) {System.out.println(o);}// 删除成员setOperations.remove("myset", "a", "b");// 取值myset = setOperations.members("myset");for (String o : myset) {System.out.println(o);}}@Testpublic void testZset() {ZSetOperations zSetOperations = redisTemplate.opsForZSet();// 存值zSetOperations.add("myZset", "a", 10.0);zSetOperations.add("myZset", "b", 11.0);zSetOperations.add("myZset", "c", 12.0);zSetOperations.add("myZset", "a", 13.0);// 取值Set<String> myZset = zSetOperations.range("myZset", 0, -1);for (String s : myZset) {System.out.println(s);}// 修改分数zSetOperations.incrementScore("myZset", "b", 20.0);// 取值myZset = zSetOperations.range("myZset", 0, -1);for (String s : myZset) {System.out.println(s);}// 删除成员zSetOperations.remove("myZset", "a", "b");// 取值myZset = zSetOperations.range("myZset", 0, -1);for (String s : myZset) {System.out.println(s);}}/*** 通用操作,针对不同的数据类型都可以操作*/@Testpublic void testCommon() {// 获取Redis中所有的keySet<String> keys = redisTemplate.keys("*");for (String key : keys) {System.out.println(key);}// 判断某个key是否存在Boolean itcast = redisTemplate.hasKey("itcast");System.out.println(itcast);// 删除指定keyredisTemplate.delete("myZset");// 获取指定key对应的value的数据类型DataType dataType = redisTemplate.type("myset");System.out.println(dataType.name());}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com