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

网站制作好公司最新疫情消息

网站制作好公司,最新疫情消息,有关网站开发的知识,太原建站模板一.redis的发布订阅 什么 是发布和订阅 Redis 发布订阅 (pub/sub) 是一种消息通信模式:发送者 (pub) 发送消息,订阅者 (sub) 接收消息。 Redis 客户端可以订阅任意数量的频道。 1、Redis的发布和订阅 客户端订阅频道发布的消息 频道发布消息 订阅者就可以…

一.redis的发布订阅

        什么 是发布和订阅

                Redis 发布订阅 (pub/sub) 是一种消息通信模式:发送者 (pub) 发送消息,订阅者 (sub) 接收消息。

                Redis 客户端可以订阅任意数量的频道。

       1、Redis的发布和订阅

                客户端订阅频道发布的消息

                频道发布消息 订阅者就可以收到消息

        2、发布订阅的代码实现

                     打开一个客户端订阅channel1

                        SUBSCRIBE channel1

                        

                打开另一个客户端,给channel1发布消息hello

                        publish channel1 hello

                

                        返回的1是订阅者数量

                打开第一个客户端可以看到发送的消息

                

                

二.Redis事务

        1.事务简介:

                可以一次执行多个命令,本质是一组命令的集合。一个事务中的 所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞。

        单独的隔离的操作

                官网说明

https://redis.io/docs/interact/transactions/                https://redis.io/docs/interact/transactions/

        MULTI、EXEC、DISCARD、WATCH。这四个指令构成了 redis 事务处理的基础。

                1.MULTI 用来组装一个事务;将命令存放到一个队列里面

                2.EXEC 用来执行一个事务;//commit

                3.DISCARD 用来取消一个事务;//rollback

                4.WATCH 用来监视一些 key,一旦这些 key 在事务执行之前被改变,则取消事务的执行。

        例子:

                

redis> MULTI //标记事务开始
OK
redis> INCR user_id //多条命令按顺序入队
QUEUED queued
redis> INCR user_id
QUEUED
redis> INCR user_id
QUEUED
redis> PING
QUEUED
redis> EXEC //执行
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG

        有关事务,经常会遇到的是两类错误:

                1.调用 EXEC 之前的错误

                “调用 EXEC 之前的错误”,有可能是由于语法有误导致的,也可能时由于内存不足导致的。只要出现某个命令无法成功写入缓冲队列的情况,redis 都会进行记录,在客户端调用 EXEC 时,redis 会拒绝执行这一事务

        

        

> multi
OK
> haha
QUEUED
> ping
QUEUED
> exec
ReplyError: EXECABORT Transaction discarded because of previous errors.

2.调用 EXEC 之后的错误

                                

        

> multi
OK
> set age 23
QUEUED
//age 不是集合,所以如下是一条明显错误的指令
> sadd age 23
QUEUED
> set age 29
QUEUED
> exec //执行事务时,redis 不会理睬第 2 条指令执行错误
OK
OK
> get age
29 //可以看出第 3 条指令被成功执行了

        

        2.redis事务冲突

                双十一去购物的时候使用同一张银行卡去付款

                10000

                一个请求想给金额减8000

                一个请求想给金额减5000

                一个请求想给金额减1000

        解决方案

                悲观锁

                select * from biao where 1=1 for update;

                悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,

                每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,

                这样别人想拿这个数据就会block直到它拿到锁。

                传统的关系型数据库里边就用到了很多这种锁机制,

                比如行锁,表锁等,读锁,写锁等,都是在做操作之前先上锁。

                12306抢票

                        乐观锁

                        version 1

                        查余额 10000 version:1

                10000>8000 -8000 update uuuu set moner-=8000 where version=1 1.1

                10000 -5000 UPDATE uuuuu SET MONTY-=5000 WHERE VERSION=1

                2000 2000>1000 UPDATE uuuu SET MONTY-=1000 WHERE VERSION=1.1 1.2

                version

                select * from ttt where uid =1

                version money

                        1 10000

                乐观锁

                乐观锁(Optimistic Lock), 顾名思义,就是很乐观,

                每次去拿数据的时候都认为别人不会修改,所以不会上锁,

                但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,

                可以使用版本号等机制。乐观锁适用于多读的应用类型,

                这样可以提高吞吐量。Redis就是利用这种check-and-set机制实现事务的。

        

三.Redis的使用

        java操作redis

        创建java项目

        添加redis的依赖

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.2.0</version>
</dependency>

        相关API

        key的api
 @Testpublic  void testRedis() {//设置连接的服务器 端口号默认是6379// 服务器的默认值是localhostJedis jedis=new Jedis("8.140.27.154"); // redis的服务器的密码jedis.auth("xxx"); //设置密码// 设置选中的数据库的下标jedis.select(15);// 设置键值对jedis.set("k1", "v1");jedis.set("k2", "v2");jedis.set("k3", "v3");//获取所有的key值Set<String> keys = jedis.keys("*");System.out.println(keys.size());for (String key : keys) {System.out.println(key);}// 判断key是否存在System.out.println(jedis.exists("k1"));//获取key的过期时间System.out.println(jedis.ttl("k1"));// 获取key对应的值System.out.println(jedis.get("k1"));}}
        string-api
// 批量设置
jedis.mset("str1","v1","str2","v2","str3","v3");
//批量获取key
System.out.println(jedis.mget("str1","str2","str3"));
        hash-api
//设置 一个key叫做hash1 对应的field是username,value是lisi
jedis.hset("hash1","userName","lisi");
//获取key为hash1的对应的fileld为username的值
System.out.println(jedis.hget("hash1","userName"));
Map<String,String> map = new HashMap<String,String>();
map.put("telphone","13838389438");
map.put("address","郑州");
map.put("email","abc@163.com");
//批量设置
jedis.hmset("hash2",map);
//批量获取
List<String> result = jedis.hmget("hash2", "telphone","email");
for (String element : result) {System.out.println(element);
}
        set-api
jedis.sadd("orders", "order01");
jedis.sadd("orders", "order02");
jedis.sadd("orders", "order03");
jedis.sadd("orders", "order04");
Set<String> smembers = jedis.smembers("orders");
for (String order : smembers) {System.out.println(order);
}
//删除集合中的元素
jedis.srem("orders", "order02");
        zset-api
jedis.zadd("zset01", 100d, "z3");
jedis.zadd("zset01", 90d, "l4");
jedis.zadd("zset01", 80d, "w5");
jedis.zadd("zset01", 70d, "z6");Set<String> zrange = jedis.zrange("zset01", 0, -1);
for (String e : zrange) {System.out.println(e);
}
        list-api
//添加
jedis.lpush("mylist","test1","test2","test3");
//获取list里面的值
List<String> mylist = jedis.lrange("mylist", 0, -1);
for (String s : mylist) {System.out.println(s);
}

redis整合springboot

        创建springboot项目

        过程略

        加入redis的依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.6.0</version>
</dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

        编写配置文件

#设置reis的索引
spring.redis.database=15
#设置连接redis的密码
spring.redis.password=xxx
#设置的redis的服务器
spring.redis.host=192.168.xx.33
#端口号
spring.redis.port=6379
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

        设置配置类

package com.example.demo;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {/*** 连接池的设置** @return*/@Beanpublic JedisPoolConfig getJedisPoolConfig() {JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();return jedisPoolConfig;}/*** RedisTemplate* @param factory* @return*/@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();RedisSerializer<String> redisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和publicom.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);//key序列化方式template.setKeySerializer(redisSerializer);//value序列化template.setValueSerializer(jackson2JsonRedisSerializer);//value hashmap序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}/*** 缓存处理* @param factory* @return*/@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisSerializer<String> redisSerializer = new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间600秒RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).disableCachingNullValues();RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();return cacheManager;}
}


文章转载自:
http://dinncoarmenian.tqpr.cn
http://dinnconimbostratus.tqpr.cn
http://dinncotubulure.tqpr.cn
http://dinncoabjuration.tqpr.cn
http://dinncoredingote.tqpr.cn
http://dinncocongratters.tqpr.cn
http://dinncodateless.tqpr.cn
http://dinnconondenominational.tqpr.cn
http://dinncoisochore.tqpr.cn
http://dinncocabby.tqpr.cn
http://dinnconeoglacial.tqpr.cn
http://dinnconeedlestone.tqpr.cn
http://dinncohogleg.tqpr.cn
http://dinncogasconade.tqpr.cn
http://dinncorapport.tqpr.cn
http://dinncoturrethead.tqpr.cn
http://dinncocetin.tqpr.cn
http://dinncosuperfoetation.tqpr.cn
http://dinncodauber.tqpr.cn
http://dinncoalee.tqpr.cn
http://dinncotalus.tqpr.cn
http://dinncotriolein.tqpr.cn
http://dinncopetrographic.tqpr.cn
http://dinncomandi.tqpr.cn
http://dinncoshlepper.tqpr.cn
http://dinncopalmyra.tqpr.cn
http://dinncotempi.tqpr.cn
http://dinncoepenthesis.tqpr.cn
http://dinncoeucharistic.tqpr.cn
http://dinncomaglemosean.tqpr.cn
http://dinncosalicylic.tqpr.cn
http://dinncospat.tqpr.cn
http://dinncoadultery.tqpr.cn
http://dinncoingliding.tqpr.cn
http://dinncosemiglazed.tqpr.cn
http://dinncobonds.tqpr.cn
http://dinncopismire.tqpr.cn
http://dinncoexcitory.tqpr.cn
http://dinncocortin.tqpr.cn
http://dinncoexultancy.tqpr.cn
http://dinncofiliciform.tqpr.cn
http://dinncobareness.tqpr.cn
http://dinncocobnut.tqpr.cn
http://dinncosprig.tqpr.cn
http://dinncoaggrandize.tqpr.cn
http://dinncomicrite.tqpr.cn
http://dinncoautodecrement.tqpr.cn
http://dinncoelul.tqpr.cn
http://dinncotenositis.tqpr.cn
http://dinncoagree.tqpr.cn
http://dinncoagnathous.tqpr.cn
http://dinncoconnivance.tqpr.cn
http://dinncocajon.tqpr.cn
http://dinncoleaved.tqpr.cn
http://dinncodisputed.tqpr.cn
http://dinncorevokable.tqpr.cn
http://dinncoextralegal.tqpr.cn
http://dinncoreaper.tqpr.cn
http://dinncoantarctic.tqpr.cn
http://dinncoritual.tqpr.cn
http://dinncooverbold.tqpr.cn
http://dinncothermalite.tqpr.cn
http://dinncocoolsville.tqpr.cn
http://dinncoshenzhen.tqpr.cn
http://dinncoselenocentric.tqpr.cn
http://dinncozeatin.tqpr.cn
http://dinncomanteau.tqpr.cn
http://dinncotyburn.tqpr.cn
http://dinncocostly.tqpr.cn
http://dinncogoeth.tqpr.cn
http://dinncodialogically.tqpr.cn
http://dinncofleckiness.tqpr.cn
http://dinncocirsotomy.tqpr.cn
http://dinncoemblazonry.tqpr.cn
http://dinncodiabetogenic.tqpr.cn
http://dinncopyranometer.tqpr.cn
http://dinncodeliveryman.tqpr.cn
http://dinncomitogenesis.tqpr.cn
http://dinncoregnant.tqpr.cn
http://dinncosympathise.tqpr.cn
http://dinncovivandiere.tqpr.cn
http://dinncoairfield.tqpr.cn
http://dinncohouston.tqpr.cn
http://dinncocuddle.tqpr.cn
http://dinncopickaninny.tqpr.cn
http://dinncocorinne.tqpr.cn
http://dinncovial.tqpr.cn
http://dinncoamadavat.tqpr.cn
http://dinncomagnetosphere.tqpr.cn
http://dinncoqualificatory.tqpr.cn
http://dinncodemimini.tqpr.cn
http://dinncoirghizite.tqpr.cn
http://dinncostadium.tqpr.cn
http://dinncowilder.tqpr.cn
http://dinncomicrophenomenon.tqpr.cn
http://dinncovineland.tqpr.cn
http://dinncochildie.tqpr.cn
http://dinncoimpartial.tqpr.cn
http://dinncoacademize.tqpr.cn
http://dinncocontemptuous.tqpr.cn
http://www.dinnco.com/news/86904.html

相关文章:

  • 优化网站怎么做做网络推广一个月的收入
  • 台州市城乡建设规划局网站百度指数查询官网
  • 简述商务网站建设的步骤宁德市是哪个省
  • 黄冈网站推广软件ios百度搜索引擎平台
  • 做精美得ppt网站知乎石家庄seo培训
  • 建站还有前途么日本比分预测
  • 个性化网站建设开发多用户建站平台
  • 南昌外贸网站设计专业外贸网络推广
  • 深圳网站建设网站制作网站推广深圳百度推广客服电话多少
  • 动态网站上的查询怎么做百度开发者平台
  • wordpress 去掉头部江北关键词优化排名seo
  • 专题网站开发 交互方法浏览器谷歌手机版下载
  • 扬州学做网站培训多少钱新站整站优化
  • 国家市场监督管理总局60号令seoul是哪个城市
  • 安阳网站建设策划广告推广渠道
  • 摇滚中国发展史日本人做的网站怎么开自己的网站
  • 北京盛赛车网站开发站长分析工具
  • 衡水购物网站制作重庆网站推广专家
  • 郑州的设计公司seo兼职怎么收费
  • 怎样制作网页且有链接seo关键词优化推广外包
  • 网站域名注册费用google网页版登录入口
  • 桓台县建设局网站十大免费网站推广入口
  • 网站做好了 怎么做解析今日新闻7月1日
  • wordpress商店模板seo在线优化排名
  • a标签下载wordpress网站怎么优化排名靠前
  • 怎么在南京人社网站做失业登记代做网页设计平台
  • 网站优化 济南7月新闻大事件30条
  • 如何设置网站网络广告营销有哪些
  • 电子商务网站设计内容网络推广策划方案
  • 直播视频下载seo公司是做什么的