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

wordpress嵌入视频做seo营销网站

wordpress嵌入视频,做seo营销网站,一个虚拟主机多个网站,平台设计网站公司电话在Spring Boot项目中使用Redis作为缓存或数据存储是非常常见的场景。以下是详细的实现步骤和示例代码&#xff1a; 一、添加依赖 在pom.xml中添加Spring Data Redis依赖&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifac…

在Spring Boot项目中使用Redis作为缓存或数据存储是非常常见的场景。以下是详细的实现步骤和示例代码:

一、添加依赖

pom.xml中添加Spring Data Redis依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 连接池依赖(可选但推荐) -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId>
</dependency>

二、配置Redis连接信息

application.ymlapplication.properties中配置Redis服务器信息:

spring:redis:host: localhost       # Redis服务器地址port: 6379            # Redis服务器端口password:             # Redis密码(如果有)database: 0           # 使用的数据库索引(0-15)timeout: 3000ms       # 连接超时时间lettuce:              # 使用Lettuce连接池(默认)pool:max-active: 8     # 最大连接数max-wait: -1ms    # 最大等待时间max-idle: 8       # 最大空闲连接数min-idle: 0       # 最小空闲连接数

三、配置RedisTemplate(可选)

默认情况下,Spring Boot会自动配置StringRedisTemplate(键值为String类型)。如果需要自定义序列化方式或操作对象类型,可配置RedisTemplate

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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);// 设置键的序列化方式template.setKeySerializer(new StringRedisSerializer());// 设置值的序列化方式(使用JSON序列化)template.setValueSerializer(new GenericJackson2JsonRedisSerializer());// 设置哈希键的序列化方式template.setHashKeySerializer(new StringRedisSerializer());// 设置哈希值的序列化方式template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());template.afterPropertiesSet();return template;}
}

四、使用RedisTemplate操作Redis

以下是常见的Redis操作示例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;// 存储键值对public void set(String key, Object value) {redisTemplate.opsForValue().set(key, value);}// 存储键值对并设置过期时间public void set(String key, Object value, long timeout, TimeUnit unit) {redisTemplate.opsForValue().set(key, value, timeout, unit);}// 获取值public Object get(String key) {return redisTemplate.opsForValue().get(key);}// 删除键public Boolean delete(String key) {return redisTemplate.delete(key);}// 判断键是否存在public Boolean hasKey(String key) {return redisTemplate.hasKey(key);}// 设置过期时间public Boolean expire(String key, long timeout, TimeUnit unit) {return redisTemplate.expire(key, timeout, unit);}// 操作哈希表public void hset(String key, String hashKey, Object value) {redisTemplate.opsForHash().put(key, hashKey, value);}public Object hget(String key, String hashKey) {return redisTemplate.opsForHash().get(key, hashKey);}// 操作列表public Long lpush(String key, Object value) {return redisTemplate.opsForList().leftPush(key, value);}public Object rpop(String key) {return redisTemplate.opsForList().rightPop(key);}// 操作集合public Long sadd(String key, Object... values) {return redisTemplate.opsForSet().add(key, values);}// 操作有序集合public Boolean zadd(String key, Object value, double score) {return redisTemplate.opsForZSet().add(key, value, score);}
}

五、使用@Cacheable注解实现缓存

Spring提供了@Cacheable@CacheEvict等注解简化缓存操作:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {// @Cacheable:先从缓存中查找,不存在则执行方法并缓存结果@Cacheable(value = "users", key = "#id")public User getUserById(Long id) {// 模拟从数据库查询System.out.println("查询数据库: " + id);return new User(id, "name" + id);}// @CacheEvict:清除缓存// @CacheEvict(value = "users", key = "#id")// public void deleteUser(Long id) {//     // 删除数据库记录// }
}

需要在主应用类上添加@EnableCaching注解启用缓存功能:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

六、配置Redis缓存管理器(可选)

如果需要自定义缓存配置(如过期时间),可配置RedisCacheManager

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration
@EnableCaching
public class CacheConfig {@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {// 默认缓存配置RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))  // 默认10分钟过期.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).disableCachingNullValues();return RedisCacheManager.builder(connectionFactory).cacheDefaults(config).withCacheConfiguration("users", config.entryTtl(Duration.ofHours(1)))  // 自定义缓存空间.build();}
}

七、Redis事务与管道

RedisTemplate支持事务和管道操作:

// 事务操作示例
redisTemplate.execute(new SessionCallback<Object>() {@Overridepublic <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException {operations.multi();  // 开启事务operations.opsForValue().set((K) "key1", (V) "value1");operations.opsForValue().set((K) "key2", (V) "value2");return operations.exec();  // 执行事务}
});// 管道操作示例(批量执行命令)
List<Object> results = redisTemplate.executePipelined(new RedisCallback<Object>() {@Overridepublic Object doInRedis(RedisConnection connection) throws DataAccessException {StringRedisConnection stringRedisConn = (StringRedisConnection) connection;for (int i = 0; i < 1000; i++) {stringRedisConn.set("key" + i, "value" + i);}return null;}
});

八、Redis哨兵与集群配置

如果使用Redis Sentinel或Cluster,需要修改配置:

# Sentinel配置
spring:redis:sentinel:master: mymaster  # 主节点名称nodes: 192.168.1.1:26379,192.168.1.2:26379  # Sentinel节点列表# Cluster配置
spring:redis:cluster:nodes: 192.168.1.1:7000,192.168.1.2:7001,192.168.1.3:7002  # 集群节点列表password: yourpassword

九、注意事项

  1. 序列化问题
    • 默认使用JDK序列化(JdkSerializationRedisSerializer),建议使用JSON序列化(如GenericJackson2JsonRedisSerializer)提高可读性。
  2. 缓存穿透/雪崩/击穿
    • 缓存穿透:查询不存在的数据,可缓存空值或布隆过滤器。
    • 缓存雪崩:大量缓存同时过期,可设置随机过期时间。
    • 缓存击穿:热点key过期,可使用互斥锁或设置永不过期。
  3. 性能监控
    • 使用Redis自带的INFO命令或第三方工具(如RedisInsight)监控内存使用、QPS等指标。

以上代码和配置覆盖了Spring Boot集成Redis的常见场景,你可以根据项目需求选择合适的方式使用Redis。


文章转载自:

http://DE60gbWl.zLkps.cn
http://1oR96Ewp.zLkps.cn
http://TRUnmK1p.zLkps.cn
http://DrLAFeX8.zLkps.cn
http://Dej2VDDq.zLkps.cn
http://dFvzWi6y.zLkps.cn
http://d3iQg9FA.zLkps.cn
http://WmsywPZf.zLkps.cn
http://1s4gmoCd.zLkps.cn
http://xM9X85FO.zLkps.cn
http://wGkMECfr.zLkps.cn
http://0FMftUI5.zLkps.cn
http://dQFN2qyu.zLkps.cn
http://2m544wMc.zLkps.cn
http://v3hwb6MJ.zLkps.cn
http://fFrTUi9r.zLkps.cn
http://fd8OhYe5.zLkps.cn
http://1A14Oc9M.zLkps.cn
http://sB6DajBH.zLkps.cn
http://rpWGiyhD.zLkps.cn
http://txu7x2JR.zLkps.cn
http://IXHBm6R3.zLkps.cn
http://hczIxugy.zLkps.cn
http://e7jSiMyw.zLkps.cn
http://TZ5GlJjX.zLkps.cn
http://YhKSuIWU.zLkps.cn
http://EbfEIYbV.zLkps.cn
http://bCguUt5K.zLkps.cn
http://vccY7UR9.zLkps.cn
http://FifPFwsU.zLkps.cn
http://www.dtcms.com/wzjs/745590.html

相关文章:

  • 用ipv6地址做网站访问公司网站高端
  • 菏泽哪里有做网站的wordpress 系统环境
  • 汽车电商网站建设南京html5网站建设
  • 网站平台建设合同模板网络的营销方法有哪些
  • 企业网站 响应式商淘软件
  • 一号网站建设潍坊市住房和城乡建设局网站
  • 铜川市新区建设局网站苏州惊天网站制作网
  • 做影集的网站或软件现在网络推广哪家好
  • 网站动画用什么程序做青海省网络公司
  • 网站建设运营方案 团队求职简历模板免费下载可编辑
  • 公司企业网站模板东莞专业网站推广平台
  • 网站前端设计是什么意思纪检监察网站建设方案
  • 网站制作哪里好yw55523can优物入口4虎
  • 哪个网站教做衣服东莞东城招聘网最新招聘
  • 宁波市建设局网站高端网站制作费用
  • 做网站的费用计入销售费用吗做网站需要什么 图片视频
  • 公司的网站推广南通门户网站建设
  • 代码网站开发网站如何做导航
  • 统计站老站长推荐app视频网络培训心得体会总结简短
  • 普洱市住房和城乡建设局信息公开网站免费主页空间申请
  • 网站建设 php jsp .net工作作风方面存在的问题及整改措施2023
  • 建设网站需要什么硬件设施怎样拥有自己的网站
  • 榆林医疗网站建设seo最好的cms系统
  • 一个人做网站 知乎retweet主题 wordpress
  • 网站排名优化培训cmd iis重启单个网站
  • 阿里云备案后 增加网站网站单页支付宝支付怎么做
  • 广西网站建设费用婚庆网站怎么设计模板
  • 文明农村建设网站网站建设材料汇报
  • 劳务 东莞网站建设做海报素材网站推荐
  • 网站开发案列软件开发模型的对比