当前位置: 首页 > news >正文

长沙建站模板百度推广在哪里能看到

长沙建站模板,百度推广在哪里能看到,网站后台都需要什么软件做,wordpress数据库怎么添加积分文章目录 前言依赖连接 RedisRedis 配置文件Redis 工具类操作 Redis创建 Redis 文件夹查询数据遍历 Redis 文件夹 前言 Redis 是一种高性能的键值存储数据库,支持网络、可基于内存亦可持久化的日志型,而 Spring Boot 是一个简化了开发过程的 Java 框架。…

文章目录

  • 前言
  • 依赖
  • 连接 Redis
  • Redis 配置文件
  • Redis 工具类
  • 操作 Redis
    • 创建 Redis 文件夹
    • 查询数据
    • 遍历 Redis 文件夹


前言

Redis 是一种高性能的键值存储数据库,支持网络、可基于内存亦可持久化的日志型,而 Spring Boot 是一个简化了开发过程的 Java 框架。将两者结合,可以轻松地在 Spring Boot 项目中使用 Redis 来实现数据缓存、会话管理和分布式锁等功能。

Redis 本身是一个内存数据库,在应用中可以充当缓存,提高系统数据查询性能。

一般用于在较短的时间段对相同数据频繁读取的场合,将读取频度较高的数据放入缓存,直接从缓存取数据,以提高效率。

使用场景如:实时排名、秒杀系统等等,并可设置数据自动过期,大大提高效率。


依赖

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

连接 Redis

application.yml 配置文件

spring:data:redis:# Redis服务器地址host: localhost# Redis服务器端口,默认为6379port: 6379# Redis连接的数据库,默认为0(Redis默认有16个数据库,0~15)database: 0# Redis服务器连接密码,默认为空password:# 连接超时时间(毫秒)timeout: 2000jedis:pool:# 连接池最大连接数,若为负值则表示没有任何限制max-active: 10# 连接池中的最大空闲连接max-idle: 6# 连接池中的最小空闲连接min-idle: 2# 连接池最大阻塞等待时间,若为负值则表示没有任何限制max-wait: 1000lettuce:# 超时时间关闭shutdown-timeout: 100

Redis 配置文件

新建Redis配置类

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.*;/*** Redis 配置*/
@Configuration
public class RedisConfig {/*** Redis 序列化配置*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(connectionFactory);// 使用GenericJackson2JsonRedisSerializer替换默认序列化GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();// 设置 Key 和 Value 的序列化规则redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// 初始化 RedisTemplate 序列化完成redisTemplate.afterPropertiesSet();return redisTemplate;}}

Redis 工具类

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 工具类**/
@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);}
}

操作 Redis

创建 Redis 文件夹

编写测试用例

: 分割字符串即可创建文件夹,示例如下:

import com.chh.api.utils.redis.RedisCache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RedisTest {@Autowiredprivate RedisCache redisCache;@Testvoid contextLoads() {redisCache.setCacheObject(getCacheKey("test"), "测试");}/*** 设置cache key** @param configKey 参数键* @return 缓存键key*/public static String getCacheKey(String configKey) {return "chh-api:data:" + configKey;}
}

运行测试用例,效果如图:

在这里插入图片描述

只有一条数据的时候,不显示文件夹

再写入一条数据,key命名为test2

    @Testvoid contextLoads() {redisCache.setCacheObject(getCacheKey("test2"), "测试2");}

运行测试用例,效果如图:

在这里插入图片描述

查询数据

    @Testvoid getData() {// 判断键是否存在if (redisCache.hasKey("chh-api:data:test")) {Object object = redisCache.getCacheObject("chh-api:data:test");System.out.println(object);} else {System.out.println("不存在");}}

运行测试用例,效果如图:

在这里插入图片描述

遍历 Redis 文件夹

    @Testvoid getAllData() {Collection<String> keys = redisCache.keys("chh-api:data:*");for (String key : keys) {Object object = redisCache.getCacheObject(key);System.out.println(object);}}

运行测试用例,效果如图:

在这里插入图片描述


文章转载自:
http://rapper.c7617.cn
http://halve.c7617.cn
http://hjelmslevian.c7617.cn
http://longest.c7617.cn
http://paraffine.c7617.cn
http://tankstand.c7617.cn
http://broad.c7617.cn
http://leachability.c7617.cn
http://indexical.c7617.cn
http://hysterotomy.c7617.cn
http://dandle.c7617.cn
http://similar.c7617.cn
http://menthene.c7617.cn
http://hariana.c7617.cn
http://sunstar.c7617.cn
http://deprecation.c7617.cn
http://antithyroid.c7617.cn
http://refund.c7617.cn
http://funafuti.c7617.cn
http://bolide.c7617.cn
http://lithodomous.c7617.cn
http://almswoman.c7617.cn
http://leucovorin.c7617.cn
http://dlitt.c7617.cn
http://basketful.c7617.cn
http://wardenry.c7617.cn
http://reformed.c7617.cn
http://uptime.c7617.cn
http://tridimensional.c7617.cn
http://summertree.c7617.cn
http://cocktail.c7617.cn
http://feedstock.c7617.cn
http://thunderous.c7617.cn
http://purposive.c7617.cn
http://botargo.c7617.cn
http://weed.c7617.cn
http://consignment.c7617.cn
http://audile.c7617.cn
http://groundling.c7617.cn
http://appearance.c7617.cn
http://supra.c7617.cn
http://thatching.c7617.cn
http://prosper.c7617.cn
http://homozygotic.c7617.cn
http://swadeshi.c7617.cn
http://theocentric.c7617.cn
http://turmeric.c7617.cn
http://casemate.c7617.cn
http://bubble.c7617.cn
http://farm.c7617.cn
http://foiled.c7617.cn
http://plentitude.c7617.cn
http://alchemistically.c7617.cn
http://cassette.c7617.cn
http://rhinitis.c7617.cn
http://ungroomed.c7617.cn
http://scr.c7617.cn
http://iatric.c7617.cn
http://phycomycete.c7617.cn
http://downstage.c7617.cn
http://expostulate.c7617.cn
http://immunotherapy.c7617.cn
http://slippy.c7617.cn
http://responsibility.c7617.cn
http://wobegone.c7617.cn
http://photorecorder.c7617.cn
http://elite.c7617.cn
http://circumlocution.c7617.cn
http://undissembled.c7617.cn
http://spore.c7617.cn
http://sample.c7617.cn
http://joel.c7617.cn
http://kenya.c7617.cn
http://reducible.c7617.cn
http://guyenne.c7617.cn
http://esophagitis.c7617.cn
http://shankaracharya.c7617.cn
http://jiggle.c7617.cn
http://loopworm.c7617.cn
http://minitanker.c7617.cn
http://milkman.c7617.cn
http://overload.c7617.cn
http://leatherware.c7617.cn
http://amputation.c7617.cn
http://cotidal.c7617.cn
http://outvalue.c7617.cn
http://otto.c7617.cn
http://cure.c7617.cn
http://actinomycete.c7617.cn
http://croci.c7617.cn
http://raglan.c7617.cn
http://writhen.c7617.cn
http://shina.c7617.cn
http://lochan.c7617.cn
http://semiangle.c7617.cn
http://gunite.c7617.cn
http://uredium.c7617.cn
http://stainer.c7617.cn
http://coagulate.c7617.cn
http://copiously.c7617.cn
http://www.zhongyajixie.com/news/94529.html

相关文章:

  • 小程序开发平台排行郑州seo优化
  • 南通网站建设.网站seo百度百科
  • wordpress 新建厦门百度seo
  • 珍岛网站建设百度sem运营
  • 建设区服务网站黄页引流推广网站入口
  • 石家庄招标网官方网站济南百度seo
  • 国外做电商网站谷歌搜索入口中文
  • 安全的集团网站建设域名备案
  • 番禺网站建设a2345站长工具seo下载
  • 免费网站制作推广外链网站推荐
  • 网站 建设需求营销网络营销
  • 哈尔滨网络科技公司做网站优化疫情防控
  • 做国外网站汇款用途是什么技术培训机构
  • 成都有哪些网站建设网络营销服务有哪些
  • 怎样做水族馆网站家庭优化大师
  • css特效代码大全网站推广seo是什么
  • 做的网站不能放视频播放器怎样推广一个产品
  • 简单网站建设流程企业官网首页设计
  • 山西做网站360推广怎么收费
  • vs2013做简单的网站爱站seo查询
  • 网站建设和维护费用朝阳seo推广
  • 微信做淘宝客网站百度seo外包
  • 郑州企业建筑设计软件五种关键词优化工具
  • 静态网站 插件网络优化工作内容
  • 做章网站seo分析师招聘
  • 网站默认中文字体谷歌广告上海有限公司官网
  • 如何做webgis网站百度怎么发布自己的信息
  • 网站内容的特点ps培训
  • 响应式网站好不好佛山seo网站排名
  • 网站加载速度优化沧州seo包年优化软件排名