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

阜宁网站建设找哪家好站长统计app软件下载

阜宁网站建设找哪家好,站长统计app软件下载,网站开发规划书,帮别人做网站开什么内容的专票引言 在现代分布式系统中,Redis 作为高性能的键值存储系统,广泛应用于缓存、消息队列、实时计数器等多种场景。然而,在高并发和分布式环境下,如何有效地管理和控制资源访问成为一个关键问题。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 RedisTemplate<String, String> redisTemplate;public void addToQueue(String queueName, String message) {ListOperations<String, String> listOps = redisTemplate.opsForList(); listOps.rightPush(queueName,  message);}public String removeFromQueue(String queueName) {ListOperations<String, 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 RedisTemplate<String, Object> redisTemplate;public void saveUser(String userId, Map<String, Object> userMap) {HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash(); hashOps.putAll(userId,  userMap);}public Map<String, Object> getUser(String userId) {HashOperations<String, 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 RedisTemplate<String, String> redisTemplate;public void addTagToUser(String userId, String tag) {SetOperations<String, String> setOps = redisTemplate.opsForSet(); setOps.add(userId,  tag);}public Set<String> getAllTags(String userId) {SetOperations<String, 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 RedisTemplate<String, String> redisTemplate;public void addScore(String rankingKey, String user, double score) {ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet(); zSetOps.add(rankingKey,  user, score);}public Set<String> getTopUsers(String rankingKey, int limit) {ZSetOperations<String, 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 依赖:

<dependencies><dependency><groupId>org.redisson</groupId> <artifactId>redisson</artifactId><version>3.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 RedisTemplate<String, 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/206050/

相关文章:

  • 网上设计接单的网站seo排名优化排行
  • wordpress后台加统计代码seo建站的步骤
  • 怎么做外贸网站的邮箱签名搜索引擎优化是指什么
  • 网页制作基础教程免费邯郸网站seo
  • phpcms做网站感想漯河seo推广
  • 公司部门kpi绩效考核指标模板河北百度seo软件
  • 印团网网站是哪家做的唯尚广告联盟
  • 网红营销网站seo综合查询怎么用的
  • 西安地区网站建设云推广
  • wordpress个人站2020年关键词排名
  • 网站建设企业公司石家庄新闻头条新闻最新今天
  • 道滘镇做网站百度统计
  • qq空间做宣传网站怎样建立自己的网站平台
  • 做设计一般用的素材网站是什么意思刷网站排名软件
  • 帮人做兼职的网站吗青岛seo服务哪家好
  • 贷款类网站怎样做网络营销的推广
  • 乐清做网站哪家好税收大数据
  • 校园网站建设需求天津放心站内优化seo
  • 哈尔滨微网站建设热搜在哪里可以看
  • 网站用oracle做数据库福州seo推广服务
  • 康保县城乡建设委员会网站营销型网站重要特点是
  • 手机做网站的步骤跨境电商有哪些平台
  • 请人做网站要多少网络事件营销
  • 网站页脚有什么作用厦门seo哪家强
  • 东莞百度提升优化优化推广网站推荐
  • 查企业网站有哪些站长统计app软件
  • 做a高清视频在线观看网站济源新站seo关键词排名推广
  • 刚做的网站怎么搜索不出来百度seo收录软件
  • 视频拍摄app站长工具seo综合查询广告
  • 新闻单位建设网站的意义武汉seo推广优化