目录
String类型:主页高频访问信息显示控制
Hash类型:双11活动日抢购活动控制
List类型:多台服务器操作日志统一顺序输出
Set类型:用户黑名单管理
ZSet类型:排行榜应用
结论
一、String类型:主页高频访问信息显示控制
应用场景
新浪微博大V主页显示粉丝数与微博数量,这类信息需要高频访问且实时更新。
代码用例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StringRedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/updateFollowers")
public String updateFollowers(String userId, int followers) {
stringRedisTemplate.opsForValue().set("user:" + userId + ":followers", String.valueOf(followers));
return "更新成功";
}
@GetMapping("/getFollowers")
public String getFollowers(String userId) {
return stringRedisTemplate.opsForValue().get("user:" + userId + ":followers");
}
}
二、Hash类型:双11活动日抢购活动控制
应用场景
商家对移动、联通、电信的30元、50元、100元商品推出抢购活动,每种商品抢购上限1000张。
代码用例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HashRedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/purchaseCard")
public String purchaseCard(String merchantId, String cardId) {
HashOperations<String, String, String> hashOperations = stringRedisTemplate.opsForHash();
if (Integer.parseInt(hashOperations.get(merchantId, cardId)) > 0) {
hashOperations.increment(merchantId, cardId, -1);
return "抢购成功";
} else {
return "抢购结束";
}
}
}
三、List类型:多台服务器操作日志统一顺序输出
应用场景
企业运营过程中,多台服务器产生的日志需要统一顺序输出。
代码用例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ListRedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/log")
public String log(String serverId, String logContent) {
ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
listOperations.leftPush("serverLogs", serverId + ": " + logContent);
return "日志记录成功";
}
@GetMapping("/getLogs")
public String getLogs() {
return stringRedisTemplate.opsForList().range("serverLogs", 0, -1).toString();
}
}
四、Set类型:用户黑名单管理
应用场景
基于经营战略设定问题用户发现、鉴别规则,周期性更新满足规则的用户黑名单。
代码用例
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SetRedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/addBlacklist")
public String addBlacklist(String userId) {
SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
setOperations.add("blacklist", userId);
return "添加黑名单成功";
}
@GetMapping("/checkBlacklist")
public boolean checkBlacklist(String userId) {
return stringRedisTemplate.opsForSet().isMember("blacklist", userId);
}
}
五、ZSet类型:排行榜应用
应用场景
实现用户积分排行榜,根据用户积分进行排序。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ZSetRedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/addScore")
public String addScore(String userId, double score) {
ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();
zSetOperations.add("userScores", userId, score);
return "添加积分成功";
}
@GetMapping("/getRank")
public Long getRank(String userId) {
ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();
return zSetOperations.reverseRank("userScores", userId);
}
}
结论
通过以上五个基础数据类型的示例,我们可以看到Redis在SpringBoot应用中的强大功能和灵活性。String类型适合存储简单的键值对,Hash类型适合存储对象,List类型可以用来实现消息队列,Set类型用于处理集合操作,而ZSet类型则适用于排序和排行榜功能。掌握这些基础数据类型,能够帮助我们在不同的业务场景中更好地使用Redis,提升系统性能。
结尾
总结Redis的五大基础数据类型的应用价值,希望可以帮助到大家日常开发。
END
我是Rookie小强一枚苟且偷生的程序员,
写代码的同时也在记录自己的成长!欢迎加我微信交个朋友!
喜欢本文的朋友们,欢迎长按下图关注订阅号成猿之路,收看更多精彩内容!
推荐阅读:
往期推荐
SpringBoot+MyBatis+MySQL读写分离(实例)