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

上土巴兔装修土巴兔装修wordpress seo标题

上土巴兔装修土巴兔装修,wordpress seo标题,个人网站建设 毕业设计,濮阳网络文章目录 一、概述二、Jedis 开发示例2.1 导入 maven 依赖2.2 使用连接池读写2.3 使用集群读写2.4 完整示例代码2.5 测试集群的搭建 三、Lettuce 开发示例3.1 导入 maven 依赖3.2 读写数据 四、Spring Boot Redis 开发示例4.1 导入 maven 依赖4.2 配置Redis服务地址4.3 基于 Re… 文章目录 一、概述二、Jedis 开发示例2.1 导入 maven 依赖2.2 使用连接池读写2.3 使用集群读写2.4 完整示例代码2.5 测试集群的搭建 三、Lettuce 开发示例3.1 导入 maven 依赖3.2 读写数据 四、Spring Boot Redis 开发示例4.1 导入 maven 依赖4.2 配置Redis服务地址4.3 基于 RedisTemplate 的读写全类型数据4.4 基于 StringRedisTemplate 的读写字符串类型数据4.5 基于 RedisConnection 的读写字节数据4.6 读写 Hash 数据类型4.7 订阅发布4.8 基于SpringBoot的完整测试代码4.9 注意问题 如果您对Redis的了解不够深入请关注本栏目本栏目包括Redis安装Redis配置文件说明Redis命令和数据类型说明Redis持久化配置Redis主从复制和哨兵机制Redis Cluster集群配置Redis Predixy 集群Redis Twemproxy 集群Redis Codis 集群Redis 集群对比RedisBloom 布隆过滤器。 一、概述 RedisRemote Dictionary Server是一种高性能的开源内存数据库它具有多种用途和功能可以充当缓存、消息队列、数据库、实时分析和数据处理平台等多种角色。具体功能如下 数据缓存 Redis 可以用作应用程序的缓存层帮助减少对后端数据库的频繁访问。通过将经常使用的数据存储在内存中可以显著提高读取速度降低数据库负担从而提高应用程序性能。会话存储 Redis 可以用于存储用户会话数据特别是在分布式环境中。这使得用户会话可以跨多个服务器实例进行共享提高了应用程序的伸缩性和可用性。消息队列 Redis 支持发布/订阅Pub/Sub模式使其成为一个优秀的消息队列平台。应用程序可以使用 Redis 来发送和接收消息实现异步通信、事件驱动和消息分发。计数器和统计信息 Redis 提供了递增和递减操作因此它非常适合存储计数器数据。这对于跟踪应用程序中的用户行为、实时统计信息和监视任务非常有用。地理空间数据 Redis 支持地理空间数据Geospatial Data因此它可以用于存储位置信息、地图数据和地理位置查询。分布式锁 Redis 可以用于实现分布式锁确保在分布式系统中的互斥操作。这对于避免竞态条件和数据一致性非常重要。缓存击穿保护 Redis 可以用于缓存击穿保护通过设置适当的过期时间或使用布隆过滤器来避免某个数据的同时大量请求导致的数据库请求。实时数据传输 Redis 可以用于构建实时数据传输和协作应用程序如聊天应用、协同编辑和游戏。数据持久性 Redis 提供不同级别的数据持久性选项以确保数据在服务器重启后不会丢失。 下面分别使用 Jedis 、Lettuce 访问Redis 和 在 Spring Boot 使用访问 Redis 的简单示例。 二、Jedis 开发示例 开源地址jedis 2.1 导入 maven 依赖 在 pom.xml 添加 jedis dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion3.9.0/version/dependency2.2 使用连接池读写 使用连接池对单个Redis实例进行读写 JedisPool pool new JedisPool(192.168.8.60, 6379);Jedis resource pool.getResource();resource.set(aaa, 111);System.out.println(read redis 1resource.get(aaa));2.3 使用集群读写 使用集群对Redis集群进行读写 SetHostAndPort jedisClusterNodes new HashSetHostAndPort();jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30001));jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30002));jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30003)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30004)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30005)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30006));JedisCluster jedis new JedisCluster(jedisClusterNodes);jedis.set(ddd, 1111);System.out.println(read redis 2 jedis.get(aaa));2.4 完整示例代码 以下是完整的测试代码 package top.yiqifu.study.p121;import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool;import java.util.HashSet; import java.util.Set;public class Test01_Redis {public static void main(String[] args) {// 通过连接池直接读写数据testResource();//通过Redis集群Cluster读写数据testCluster();}private static void testResource(){JedisPool pool new JedisPool(192.168.8.60, 6379);Jedis resource pool.getResource();resource.set(aaa, 111);System.out.println(read redis 1resource.get(aaa));}private static void testCluster(){SetHostAndPort jedisClusterNodes new HashSetHostAndPort();jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30001));jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30002));jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30003)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30004)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30005)); // jedisClusterNodes.add(new HostAndPort(192.168.8.60, 30006));JedisCluster jedis new JedisCluster(jedisClusterNodes);jedis.set(ddd, 1111);System.out.println(read redis 2 jedis.get(aaa));} }2.5 测试集群的搭建 以下给出测试集群搭建的核心命令具体请参考Redis Cluster集群配置。 cd /redis-6.0.6/utils/create-cluster vi create-clusterCLUSTER_HOST192.168.8.60PROTECTED_MODEno ./create-cluster start ./create-cluster createfirewall-cmd --permanent --add-port30001/tcp firewall-cmd --permanent --add-port30002/tcp firewall-cmd --permanent --add-port30003/tcp firewall-cmd --permanent --add-port30004/tcp firewall-cmd --permanent --add-port30005/tcp firewall-cmd --permanent --add-port30006/tcp firewall-cmd --reload三、Lettuce 开发示例 3.1 导入 maven 依赖 开源地址lettuce-core 在 pom.xml 添加 lettuce-core dependencygroupIdio.lettuce/groupIdartifactIdlettuce-core/artifactIdversion6.1.10.RELEASE/version/dependency3.2 读写数据 以下使用 lettuce 来读写Redislettuce 最大的特点是支持响应式编程Reactive API。 package top.yiqifu.study.p121;import io.lettuce.core.RedisClient; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.async.RedisAsyncCommands; import io.lettuce.core.api.sync.RedisStringCommands;public class Test02_LettuceRedis {public static void main(String[] args) {// 同步/异步方式读写数据testSync();}private static void testSync(){RedisClient client RedisClient.create(redis://192.168.8.60:6379);StatefulRedisConnectionString, String connection client.connect();RedisStringCommands sync connection.sync();sync.set(aaa, 111);System.out.println(read redis 1sync.get(aaa));RedisAsyncCommandsString, String async connection.async();async.set(bbb, 222);System.out.println(read redis 2async.get(bbb));} } 四、Spring Boot Redis 开发示例 4.1 导入 maven 依赖 这里 spring-boot-starter-data-redis 是 Redis 的依赖而 spring-boot-starter-json 是用于数据序列化的 json 依赖。 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdversion2.7.15/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-json/artifactIdversion2.7.15/version/dependency4.2 配置Redis服务地址 在 application.yaml 文件中添加配置 spring:redis:host: 192.168.8.60port: 63794.3 基于 RedisTemplate 的读写全类型数据 AutowiredRedisTemplate redisTemplate;private void testObject(){redisTemplate.opsForValue().set(aaa, 111);System.out.println(reda redis 1 redisTemplate.opsForValue().get(aaa));}4.4 基于 StringRedisTemplate 的读写字符串类型数据 AutowiredStringRedisTemplate stringRedisTemplate;private void testString(){stringRedisTemplate.opsForValue().set(bbb, 222);System.out.println(read redis 2 stringRedisTemplate.opsForValue().get(bbb));}4.5 基于 RedisConnection 的读写字节数据 AutowiredRedisTemplate redisTemplate;private void testObject(){redisTemplate.opsForValue().set(aaa, 111);System.out.println(reda redis 1 redisTemplate.opsForValue().get(aaa));}4.6 读写 Hash 数据类型 AutowiredRedisTemplate redisTemplate;AutowiredStringRedisTemplate stringRedisTemplate;AutowiredQualifier(serializerRedisTemplate)StringRedisTemplate serializerRedisTemplate;private void testHash(){// 方法一使用 StringRedisTemplate 直接读写hashHashOperationsString, Object, Object hash stringRedisTemplate.opsForHash();hash.put(someInfo, name , qifu);hash.put(someInfo, age , 30);System.out.println(read redis 4 hash.entries(someInfo));//hincrby someInfo age 1// 创建对象Person person new Person();person.setName(zhang san);person.setAge(20);Jackson2HashMapper hashMapper new Jackson2HashMapper(objectMapper, false);// 方法二使用 RedisTemplate 读写 hash 对象redisTemplate.opsForHash().putAll(person1, hashMapper.toHash(person));Map personMap1 redisTemplate.opsForHash().entries(person1);Person value6 objectMapper.convertValue(personMap1, Person.class);System.out.println(read redis 6 value6.getName());// 方法三使用 StringRedisTemplate 读写 hash 对象// stringRedisTemplate 需设置 ValueSerializer 因为age是Integer类型stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializerObject(Object.class));stringRedisTemplate.opsForHash().putAll(person2, hashMapper.toHash(person));Map personMap2 stringRedisTemplate.opsForHash().entries(person2);Person value7 objectMapper.convertValue(personMap2, Person.class);System.out.println(read redis 7 value7.getName());// 方法四使用自定义 serializerRedisTemplate 读写 hash 对象serializerRedisTemplate.opsForHash().putAll(person3, hashMapper.toHash(person));Map personMap3 serializerRedisTemplate.opsForHash().entries(person3);Person value8 objectMapper.convertValue(personMap3, Person.class);System.out.println(read redis 8 value8.getName());}4.7 订阅发布 AutowiredStringRedisTemplate stringRedisTemplate;private void testPubsub(){//pub/substringRedisTemplate.getConnectionFactory().getConnection().subscribe(new MessageListener() {Overridepublic void onMessage(Message message, byte[] pattern) {System.out.println(sub redis new String(message.getBody()));}}, xxx.getBytes());stringRedisTemplate.convertAndSend(xxx,yyy);}4.8 基于SpringBoot的完整测试代码 TestRedis.java package top.yiqifu.study.p211_redis;import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.hash.Jackson2HashMapper; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.stereotype.Component;import java.util.Map;Component public class TestRedis {AutowiredRedisTemplate redisTemplate;AutowiredStringRedisTemplate stringRedisTemplate;AutowiredQualifier(serializerRedisTemplate)StringRedisTemplate serializerRedisTemplate;AutowiredObjectMapper objectMapper;public void test(){// 基于 RedisTemplate 测试Redis全类型的读写如 objectthis.testObject();// 基于 StringRedisTemplate 测试Redis字符串类型的读写如 stringthis.testString();// 基于 RedisConnection 测试Redis更底层的字节类型的读写如 byte[]this.testBytes();// 基于 StringRedisTemplate 测试Redis的Hash类型的读写如 hashthis.testHash();// 基于 StringRedisTemplate 测试Redis的发布、订阅this.testPubsub();}private void testObject(){redisTemplate.opsForValue().set(aaa, 111);System.out.println(reda redis 1 redisTemplate.opsForValue().get(aaa));}private void testString(){stringRedisTemplate.opsForValue().set(bbb, 222);System.out.println(read redis 2 stringRedisTemplate.opsForValue().get(bbb));}private void testBytes(){RedisConnection connection redisTemplate.getConnectionFactory().getConnection();connection.set(ccc.getBytes(), 333.getBytes());System.out.println(read redis 3 new String(connection.get(ccc.getBytes())));}private void testHash(){// 方法一使用 StringRedisTemplate 直接读写hashHashOperationsString, Object, Object hash stringRedisTemplate.opsForHash();hash.put(someInfo, name , qifu);hash.put(someInfo, age , 30);System.out.println(read redis 4 hash.entries(someInfo));//hincrby someInfo age 1// 创建对象Person person new Person();person.setName(zhang san);person.setAge(20);Jackson2HashMapper hashMapper new Jackson2HashMapper(objectMapper, false);// 方法二使用 RedisTemplate 读写 hash 对象redisTemplate.opsForHash().putAll(person1, hashMapper.toHash(person));Map personMap1 redisTemplate.opsForHash().entries(person1);Person value6 objectMapper.convertValue(personMap1, Person.class);System.out.println(read redis 6 value6.getName());// 方法三使用 StringRedisTemplate 读写 hash 对象// stringRedisTemplate 需设置 ValueSerializer 因为age是Integer类型stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializerObject(Object.class));stringRedisTemplate.opsForHash().putAll(person2, hashMapper.toHash(person));Map personMap2 stringRedisTemplate.opsForHash().entries(person2);Person value7 objectMapper.convertValue(personMap2, Person.class);System.out.println(read redis 7 value7.getName());// 方法四使用自定义 serializerRedisTemplate 读写 hash 对象serializerRedisTemplate.opsForHash().putAll(person3, hashMapper.toHash(person));Map personMap3 serializerRedisTemplate.opsForHash().entries(person3);Person value8 objectMapper.convertValue(personMap3, Person.class);System.out.println(read redis 8 value8.getName());}private void testPubsub(){//pub/substringRedisTemplate.getConnectionFactory().getConnection().subscribe(new MessageListener() {Overridepublic void onMessage(Message message, byte[] pattern) {System.out.println(sub redis new String(message.getBody()));}}, xxx.getBytes());stringRedisTemplate.convertAndSend(xxx,yyy);} }Config.java package top.yiqifu.study.p211_redis;import org.springframework.beans.factory.annotation.Autowired; 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.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import javax.annotation.Resource;Configuration public class Config {ResourceRedisConnectionFactory factory;Bean(serializerRedisTemplate)public StringRedisTemplate getRedisTemplate(){StringRedisTemplate template new StringRedisTemplate(factory);template.setHashValueSerializer(new Jackson2JsonRedisSerializerObject(Object.class));return template;} }RedisSpringBootApplication.java package top.yiqifu.study.p211_redis;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext;SpringBootApplication public class RedisSpringBootApplication {public static void main(String[] args){ConfigurableApplicationContext context SpringApplication.run(RedisSpringBootApplication.class, args);TestRedis bean context.getBean(TestRedis.class);bean.test();} }4.9 注意问题 在新版本版本大于2.7的SpringBoot中以下写法会报错“Could not autowire. No beans of ‘RedisConnectionFactory’ type found”如下写法 Configuration public class Config {Bean(serializerRedisTemplate)public StringRedisTemplate getRedisTemplate(RedisConnectionFactory factory){StringRedisTemplate template new StringRedisTemplate(factory);template.setHashValueSerializer(new Jackson2JsonRedisSerializerObject(Object.class));return template;} }要使用Resource注解的属性注入修改后的代码为 Configuration public class Config {ResourceRedisConnectionFactory factory;Bean(serializerRedisTemplate)public StringRedisTemplate getRedisTemplate(){StringRedisTemplate template new StringRedisTemplate(factory);template.setHashValueSerializer(new Jackson2JsonRedisSerializerObject(Object.class));return template;} }
http://www.yutouwan.com/news/385376/

相关文章:

  • 苏州网站建设外贸甜品网站设计
  • 浙江个人网站备案静安做网站公司
  • 白和黑人做网站wordpress 主题加密
  • 旅游网站毕业设计和论文南昌网站免费制作
  • 网站推广常用的方法公众号怎么开通直播
  • 音乐网站html模板wordpress洗发水主题
  • 做企业网站费用wordpress mysql 优化
  • 旅游网站设计图软件开发人员工资标准
  • 做网站赚钱流程中国建筑网官网企业文化
  • 做网站的成本有多少北京seo专员
  • 重庆市建设工程施工安全管理总站旅游网站建设规范
  • 电商网站建设试题网络游戏带来的危害
  • 做企业网站需要买什么营销型网站的网址
  • 九龙坡区建设二校有网站吗域名查询whois
  • 郑州建设网站制作公司网站开发原创动漫
  • 未备案网站个人作品主页wordpress
  • 齐齐哈尔网站设计飞鱼crm系统
  • 学校网站建设与维护南京做网站工作室
  • 北京做网站哪家专业网站注册域名与制作
  • 网站建设私人合同范本网站设计教程
  • 在商用网站上用明星的名字做昵称网页设计美化教程
  • 营销型企业网站名词解释分销网站建站
  • 免费的网页入口沧州网站建设 3tseo
  • 长沙手机网站开发旅游网站开发哪家好
  • 公司建网站哪家开发公司经营范围
  • asp网站建设 aws百度推广去哪里学技术
  • 肉菜配送网站建设做网站哪家公司便宜
  • 创建个人主页网站射洪网站建设工作室
  • 晋中网站开发小程序搭建赚钱吗
  • 做招聘网站的要求外贸皮包网站模板