上传到ftp网站模板,安卓市场下载官网,wordpress缩略图不显示,百度指数有什么作用Redis简介
Redis#xff08;Remote Dictionary Server#xff09;#xff0c;即远程字典服务#xff0c;是一个开源的、使用ANSI C语言编写的、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库#xff0c;并提供多种语言的API。
功能特点
数据结构丰富#…Redis简介
RedisRemote Dictionary Server即远程字典服务是一个开源的、使用ANSI C语言编写的、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库并提供多种语言的API。
功能特点
数据结构丰富Redis支持多种数据结构包括string字符串、list链表、set集合、zsetsorted set --有序集合和hash哈希类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集等丰富的操作且这些操作都是原子性的。高性能Redis将数据存储在内存中因此其读写速度非常快。根据官方数据Redis可以轻松支持超过10万次QPS每秒查询率的读写频率。持久化虽然Redis主要将数据存储在内存中但它也提供了持久化的功能。Redis可以周期性地将更新的数据写入磁盘或将修改操作写入追加的记录文件以确保数据的持久性。主从同步和复制Redis支持主从同步数据可以从主服务器向任意数量的从服务器上同步。从服务器可以是关联其他从服务器的主服务器这使得Redis可执行单层树复制。哨兵(Sentinel)和集群(Cluster)Sentinel可以管理多个Redis服务器提供监控、提醒以及自动的故障转移功能。Redis Cluster则允许将数据分布在多个Redis节点上以实现横向扩展和负载均衡。分布式锁Redis还可以用作分布式锁以确保在分布式系统中对共享资源的访问是同步的。发布/订阅机制Redis完全实现了发布/订阅机制允许从数据库在任何地方同步树时订阅一个频道并接收主服务器完整的消息发布记录。
应用场景
如缓存服务、消息队列、分布式锁等。由于其高性能和丰富的功能Redis已经成为许多主流架构中不可或缺的一部分。
关于Redis的安装和使用可以根据不同的操作系统和需求选择相应的方法。例如在Windows系统中可以通过解压并双击redis-server.exe和redis-cli.exe来启动服务器和命令窗口。在Linux系统中则可以按照详细的安装部署教程进行安装和配置。同时Redis也提供了丰富的命令和API以满足不同的使用需求。
Redis安装
笔者是在Macos系统下采用HomeBrew进行的安装
安装
brew install redis启动
brew启动
brew services list
brew services start redis
brew services stop redis
ps -ef | grep redisredis命令启动
# 启动
redis-server# 客户端工具
redis-cli# 设置密码
CONFIG SET requirepass YOUR_PASSWORD
auth YOUR_PASSWORDspringboot-redis模块
模块工程结构 pom.xml
依赖版本可参考 springbootSeries 模块中pom.xml文件中的版本定义 dependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-consul-discovery/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency/dependenciesapplication.properties配置
spring.profiles.active dev
spring.application.namespringbootRedis
server.port18087spring.cloud.consul.hostlocalhost
spring.cloud.consul.port8500
spring.cloud.consul.discovery.service-name${spring.application.name}spring.data.redis.hostlocalhost
spring.data.redis.port6379
spring.data.redis.passwordSpringbootApplication启动类
添加 EnableDiscoveryClient 注解
Slf4j
SpringBootApplication(scanBasePackages {com.korgs, cn.hutool.extra.spring})
Configuration
EnableConfigurationProperties
ServletComponentScan
RestController
EnableDiscoveryClient
public class SpringbootRedisApplication {public static void main(String[] args) {SpringApplication.run(SpringbootRedisApplication.class, args);}GetMapping(/helloworld)public BaseResponse helloWorld(){log.info(LogGenerator.trackLog() msg I am busy to handle this request.);return BaseResponse.success(hello world);}
}java-redis实现
Redis bean实现
在使用时有两个模板配置Redis适用于需要持久化、复杂数据结构和高性能的场景而Cache Manager适用于简单的缓存需求部署和管理相对简单。具体使用哪种方案应根据实际需求和场景来决定。
Configuration
public class RedisConfig {BeanRedisConnectionFactory connectionFactory() {return new LettuceConnectionFactory();}BeanRedisTemplateString, String redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateString, String template new RedisTemplate();template.setConnectionFactory(connectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer( new JdkSerializationRedisSerializer());return template;}BeanStringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {StringRedisTemplate template new StringRedisTemplate();template.setConnectionFactory(redisConnectionFactory);return template;}
}RedisController实现
Slf4j
RestController
RequestMapping(/api/load)
public class RedisController {//Pushing an item to a List using [Reactive]RedisTemplateResource(nameredisTemplate)private ListOperationsString, String listOps;//String-focused Convenience ClassesAutowiredprivate StringRedisTemplate redisTemplate;GetMapping(/v1/listOperator)public BaseResponseString listOperator(String uuid, String url){log.info(LogGenerator.trackLog() uuid : listOps.size(uuid));String cache listOps.index(uuid, 0);if (StrUtil.isEmpty(cache)){listOps.leftPush(uuid, url);return BaseResponse.success(uuid : url);}listOps.leftPush(uuid, url);return BaseResponse.success(uuid : cache);}GetMapping(/v1/stringOperator)public BaseResponseString stringOperator(String uuid, String url){String cache redisTemplate.opsForValue().get(uuid);log.info(LogGenerator.trackLog() uuid : cache);if (StrUtil.isEmpty(cache)){redisTemplate.opsForValue().set(uuid, url);return BaseResponse.success(uuid : url);}redisTemplate.opsForValue().set(uuid, url);return BaseResponse.success(uuid : cache);}//how to use the RedisCallback interfacepublic void useCallback() {redisTemplate.execute(new RedisCallbackObject() {public Object doInRedis(RedisConnection connection) throws DataAccessException {Long size connection.dbSize();// Can cast to StringRedisConnection if using a StringRedisTemplate((StringRedisConnection)connection).set(key, value);return size;}});}
}源码下载
涉及模块
springboot-redis:18087
源码下载
基础框架源码下载Springboot集成Redis操作缓存
源码运行方法
模块详细功能说明和运行测试方法