惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - XXXTaye

02-Java中的锁详解 01-Java内存模型 01-redis基础认知 06-反射 05-继承 07-接口 01-mysql必知必会 01-java基础补充
02-redis应用
XXXTaye · 2021-11-02 · via 博客园 - XXXTaye

发步和订阅

发布者:发布消息

订阅者:得到订阅的发布者发布的消息

redis客户端可以订阅任意多的频道

使用subscribe订阅频道和publish发布频道

客户端A订阅一个channel1:

image-20211017235354300

另一个客户端B发布内容:

image-20211017235439418

客户端A收到消息:

image-20211017235515475

redis6新的数据类型

Bitmaps

Bitmaps本身是一个字符串,可以对字符串进行位操作

setbit <key> <offset> <value> # 设置偏移量offset位置的值为value
getbit <key> <offset> # 取偏移量offset处的值
bitcount <start> <end> # 统计值为1的bit数,默认统计整个字符串,可设置开始和结束位置
bitop and/or/not/xor k [k1 k2 ...] # 进行与或非和异或等操作,结果存到k

image-20211018001257770

注意:offset的偏移从0开始

HyperLogLog

HyperLogLog只会根据输入元素计算基数,不会存储输入元素

比如这样一个集[1,1,2,2,3,3],他的集合是{1,2,3},基数为3

pfadd <key> v1 v2... # 加入一组元素
pfcount k # 近似计算基数
pfmerge k1 k2 k3... # 把后面的基数合并到k1中

Geographic

地理信息,也就是元素的2维坐标,也就是经纬度

geoadd k 经度 纬度 地理名 # 添加一个地理位置的经纬度
geopos k 地理名 # 获取地理位置的经纬度
geodist k 地理1 地理2 单位# 得到两个位置间的直线距离
georadius k 经度 纬度 半径 # 取某个位置半径内的地理位置

Jedis

使用java操作redis的工具,先导入依赖:

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

外部连接redis服务器,要修改一些默认配置:

  1. 修改bind属性,直接注释掉
  2. 设置protected-mode为no
  3. 使用阿里云的话,记得去服务器打开对应端口

使用jedis方法:

  1. 获取jedis对象

    Jedis jedis = new Jedis(url,port);
    
  2. 进行各种操作,比如存值取值等,下面就是存一个List然后取出来的操作

    jedis.lpush("myList","dog","cat","cow","tiger");
    List<String> list = jedis.lrange("myList",0,-1);
    

基于Jedis的手机验证码案例

要求:

  1. 输入手机号,随机生成6位数字,2分钟内有效
  2. 输入验证码进行验证
  3. 每个手机只能申请发送3次

思路:

  1. 生成的数字放到redis,设置2min过期时间
  2. 从redis中取出来进行验证
  3. 把手机号也存入redis,每次发送incr,大于2则不能发送
public class JedisPhone {
    public static void main(String...args){
        JedisPhone phone = new JedisPhone();
        phone.sendCode("18982137871");
        System.out.println(phone.checkCode("18982137871","288308"));
    }

    // 1. 发送手机的验证码
    public void sendCode(String phone){
        String countKey = "Count:"+ phone;
        Jedis jedis = new Jedis("XXX",6379);  //ip地址保密
        String count = jedis.get(countKey);
        if(count == null){
            jedis.set(countKey, String.valueOf(0));
        }else if(Integer.parseInt(count) <=1){	//null->0->1
            jedis.incr(countKey);
        }else{
            System.out.println("发送次数已达到上限");
        }

        String codeKey = "Code:" + phone;
        StringBuilder code = new StringBuilder("");
        Random random = new Random();
        for (int i=0; i<6; i++){
            int temp = random.nextInt(10);
            code.append(temp);
        }
        jedis.set(codeKey, code.toString());

        jedis.close();
    }

    // 2. 输入验证码进行验证
    public boolean checkCode(String phone, String code){
        Jedis jedis = new Jedis("xxx",6379);  //ip地址保密
        String codeKey = "Code:" + phone;
        if(code.equals(jedis.get(codeKey))){
            jedis.close();
            return true;
        }
        jedis.close();
        return false;
    }
}