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

网站如何强制修改主页 源码美食网站页面设计源代码

网站如何强制修改主页 源码,美食网站页面设计源代码,软件开发外包网站,沈阳快速建站公司有哪些引言 在现代分布式系统中#xff0c;Redis 作为高性能的键值存储系统#xff0c;广泛应用于缓存、消息队列、实时计数器等多种场景。然而#xff0c;在高并发和分布式环境下#xff0c;如何有效地管理和控制资源访问成为一个关键问题。Redis 分布式锁正是为了解决这一问题…引言 在现代分布式系统中Redis 作为高性能的键值存储系统广泛应用于缓存、消息队列、实时计数器等多种场景。然而在高并发和分布式环境下如何有效地管理和控制资源访问成为一个关键问题。Redis 分布式锁正是为了解决这一问题而诞生的技术。 本文将从 Redis 的数据结构应用入手结合 Redisson 分布式锁的实现深入探讨如何解决常见的缓存问题如穿透、击穿、雪崩并提供详尽的代码示例和注释。 一、Redis 数据结构应用 Redis 提供了多种数据结构每种数据结构都有其特定的应用场景。以下是几种常见数据结构及其典型应用场景 1. String字符串 应用场景适用于简单的键值存储如用户会话、计数器等。示例代码 import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; Service public class CounterService {Autowired private StringRedisTemplate stringRedisTemplate;public void incrementCounter(String key) {stringRedisTemplate.opsForValue().increment(key, 1);}public Long getCounter(String key) {return stringRedisTemplate.opsForValue().get(key); } } increment(key, 1)原子递增计数器。get(key)获取计数器的值。 2. List列表 应用场景适用于队列或栈结构如消息队列、任务队列等。示例代码 import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class QueueService {Autowired private RedisTemplateString, String redisTemplate;public void addToQueue(String queueName, String message) {ListOperationsString, String listOps redisTemplate.opsForList(); listOps.rightPush(queueName, message);}public String removeFromQueue(String queueName) {ListOperationsString, String listOps redisTemplate.opsForList(); return listOps.leftPop(queueName); } } rightPush(queueName, message)将消息添加到队列尾部。leftPop(queueName)从队列头部取出消息。 3. Hash哈希 应用场景适用于存储对象或映射表如用户信息、商品详情等。示例代码 import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class UserService {Autowired private RedisTemplateString, Object redisTemplate;public void saveUser(String userId, MapString, Object userMap) {HashOperationsString, String, Object hashOps redisTemplate.opsForHash(); hashOps.putAll(userId, userMap);}public MapString, Object getUser(String userId) {HashOperationsString, String, Object hashOps redisTemplate.opsForHash(); return hashOps.entries(userId); } } putAll(userId, userMap)将用户信息存储到哈希中。entries(userId)获取用户的完整信息。 4. Set集合 应用场景适用于存储唯一元素的集合如用户关注列表、标签分类等。示例代码 import org.springframework.data.redis.core.SetOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class TagService {Autowired private RedisTemplateString, String redisTemplate;public void addTagToUser(String userId, String tag) {SetOperationsString, String setOps redisTemplate.opsForSet(); setOps.add(userId, tag);}public SetString getAllTags(String userId) {SetOperationsString, String setOps redisTemplate.opsForSet(); return setOps.members(userId); } } add(userId, tag)向用户的标签集合中添加一个标签。members(userId)获取用户的全部标签。 5. ZSet有序集合 应用场景适用于需要排序的场景如排行榜、优先级队列等。示例代码 import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class RankingService {Autowired private RedisTemplateString, String redisTemplate;public void addScore(String rankingKey, String user, double score) {ZSetOperationsString, String zSetOps redisTemplate.opsForZSet(); zSetOps.add(rankingKey, user, score);}public SetString getTopUsers(String rankingKey, int limit) {ZSetOperationsString, String zSetOps redisTemplate.opsForZSet(); return zSetOps.reverseRange(rankingKey, 0, limit);} } add(rankingKey, user, score)向排行榜中添加用户及其分数。reverseRange(rankingKey, 0, limit)获取排行榜前几名的用户。 二、Redisson 分布式锁 1. 什么是 Redisson Redisson 是一个 Redis 的 Java 客户端提供了许多高级功能包括分布式锁、分布式集合、分布式消息队列等。它简化了 Redis 的使用并提供了丰富的功能。 2. 分布式锁的应用场景 在分布式系统中多个服务实例可能同时访问共享资源如数据库、文件等这可能导致数据不一致或竞争条件。分布式锁可以确保在同一时间只有一个服务实例能够访问共享资源。 3. 使用 Redisson 实现分布式锁 步骤 1添加依赖 在 pom.xml 中添加 Redisson 依赖 dependenciesdependencygroupIdorg.redisson/groupId artifactIdredisson/artifactIdversion3.17.6/version/dependency /dependencies 步骤 2配置 Redisson 在配置类中配置 Redisson 客户端 import org.redisson.Redisson; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class RedissonConfig {Bean public Redisson redisson() {Config config new Config();config.useSingleServer() .setAddress(redis://localhost:6379);return Redisson.create(config); } } 步骤 3实现分布式锁 import org.redisson.api.RLock; import org.redisson.api.Redisson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; Service public class DistributedLockService {Autowired private Redisson redisson;public void executeWithLock(String lockName) {RLock lock redisson.getLock(lockName); try {boolean isLocked lock.tryLock(10, 1000, TimeUnit.MILLISECONDS);if (isLocked) {// 执行临界区代码 System.out.println(Lock acquired. Executing critical section...);Thread.sleep(2000); // 模拟耗时操作 } else {System.out.println(Failed to acquire lock.);}} catch (InterruptedException e) {Thread.currentThread().interrupt(); } finally {if (lock.isHeldByCurrentThread()) {lock.unlock(); }}} } tryLock(10, 1000, TimeUnit.MILLISECONDS)尝试获取锁最长等待 10 秒每次轮询间隔 1 秒。unlock()释放锁。 步骤 4测试分布式锁 RunWith(SpringRunner.class) SpringBootTest public class DistributedLockServiceTest {Autowired private DistributedLockService distributedLockService;Test public void testDistributedLock() throws InterruptedException {// 同时启动多个线程尝试获取锁 Runnable task () - distributedLockService.executeWithLock(my_lock); Thread thread1 new Thread(task);Thread thread2 new Thread(task);thread1.start(); thread2.start(); thread1.join(); thread2.join(); } } 运行后控制台将显示只有其中一个线程成功获取锁并执行临界区代码。 三、缓存问题解决方案 在实际应用中缓存可能会遇到以下问题 1. 缓存穿透 问题描述查询一个不存在的数据导致每次都去数据库查询。解决方案 缓存空值将不存在的数据也缓存起来。布隆过滤器预先过滤不存在的数据。 示例代码缓存空值 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class UserService {Cacheable(value users, key #id)public User getUserById(Long id) {User user userRepository.findById(id).orElse(null); if (user null) {// 缓存空值 return new User();}return user;} } 2. 缓存击穿 问题描述高并发下同一个热点数据过期导致大量请求同时访问数据库。解决方案 互斥锁加延迟过期在更新缓存时加锁避免多个请求同时更新。永不过期通过版本号或其他方式实现逻辑过期。 示例代码互斥锁加延迟过期 import org.redisson.api.RLock; import org.redisson.api.Redisson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; Service public class UserService {Autowired private Redisson redisson;Autowired private UserRepository userRepository;public User getUserById(Long id) {String key user: id;String value redisTemplate.opsForValue().get(key); if (value ! null) {return JSON.parseObject(value, User.class); }RLock lock redisson.getLock(lock: id);try {boolean isLocked lock.tryLock(10, 1000, TimeUnit.MILLISECONDS);if (isLocked) {value redisTemplate.opsForValue().get(key); if (value ! null) {return JSON.parseObject(value, User.class); }User user userRepository.findById(id).orElse(null); if (user ! null) {redisTemplate.opsForValue().set(key, JSON.toJSONString(user), 3600L, TimeUnit.SECONDS);} else {// 缓存空值 redisTemplate.opsForValue().set(key, , 3600L, TimeUnit.SECONDS);}}} catch (InterruptedException e) {Thread.currentThread().interrupt(); } finally {if (lock.isHeldByCurrentThread()) {lock.unlock(); }}return user ! null ? user : new User();} } 3. 缓存雪崩 问题描述大量缓存同时过期导致数据库压力骤增。解决方案 随机过期时间为每个缓存设置不同的过期时间。永不过期通过版本号或其他方式实现逻辑过期。 示例代码随机过期时间 import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; Service public class CacheService {Autowired private RedisTemplateString, Object redisTemplate;public void setValueWithRandomExpire(String key, Object value) {long randomExpireTime 3600L (long) (Math.random() * 3600); // 随机过期时间1-2小时redisTemplate.opsForValue().set(key, value, randomExpireTime, TimeUnit.SECONDS);} }
http://www.hkea.cn/news/14336849/

相关文章:

  • 瑞安哪里有培训做网站的网站建设域名服务器
  • 有做网站的吗 优帮云怎么找客户资源
  • 上海网站建设的网可以做视频的一个网站
  • 后端网站开发Wordpress不同分类下分页
  • 做网站就用建站之星网站建设需要什么流程图
  • 机械设备如何做网站前端和后端是什么意思
  • 域名访问网站张家界商城网站建设
  • 域名服务商网站三亚网站建设品牌
  • 负责网站建设和网络推广的重庆网站租赁空间
  • 网站如何做支付系统关键词指数批量查询
  • 企业营销策划 网站建设新网域名注册步骤
  • 苏州网站建设设计产品是做网站
  • 百度网站官网网址腾讯云域名备案需要提供网站建设方案书
  • 适合初学者做的网站保定php网站制作
  • 动漫网站建设策划书wordpress 火车头发布规则
  • 建设网站企业排行鲜花网站建设的利息分析
  • 做国外网站什么定位免费海外网站cdn加速
  • 租号网站开发阜城网站建设代理
  • 中国质量建设协会网站亚马逊网站推广怎么做
  • asp.net 网站开发架构太仓网站建设公司
  • 网站与新媒体建设测评方案大连开发区社保网站
  • 东莞网站优化推荐软装设计费用
  • 好看的设计网站wordpress和织梦哪个好
  • 威海网站优化沈阳建站模板展示
  • 如何建设一个静态网站基础建设文本网站
  • 做两个阿里网站吗网站建设 品牌塑造计划
  • 高安网站制作荆门网站制作
  • 越影网站建设做网站SEO用什么电脑方便
  • 行业门户网站建设方案设计了网站
  • 摄影网站开发背景怎么写顺德制作网站