首页 > 其他 > 详细

Redis入门

时间:2020-04-27 23:15:23      阅读:64      评论:0      收藏:0      [点我收藏+]

Redis简介

  • REmote DIctionary Server

技术分享图片

技术分享图片

Redis安装与常用配置

安装

wget http://download.redis.io/releases/redis-5.0.8.tar.gz
tar xzf redis-5.0.8.tar.gz
cd redis-5.0.8
make

基本配置

技术分享图片

数据类型

String

技术分享图片

技术分享图片

Hash

技术分享图片

技术分享图片

List

技术分享图片

rpush listkey c b a  -右侧插入
lpush listkey f e d  -左侧插入
rpop listkey         -右侧弹出
lpop listkey         -左侧弹出
lrange listkey 0 -1  -查询所有

Set与Zset

技术分享图片

sadd set1 c       -插入
smembers set1     -查看
sinter set1 set2  -交集查询
sunion set1 set2  -并集查询
sdiff set1 set2   -差集查询

zadd zset1 99 c               -插入
zrange zset1 0 -1             -查询所有
zrange zset1 0 -1 withscores  -查询包含分数
zrangebyscore zset1 100 101   -范围查询

Java中使用Redis

Jedis介绍

技术分享图片

Jedis操作String、Hash和List

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
package com.jesse;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JedisTestor {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.2.4",6379);
        try{
            jedis.auth("123");
            jedis.select(4);
            System.out.println("Redis连接成功");
            //String
            jedis.set("sn","7781-9938");
            String sn = jedis.get("sn");
            System.out.println(sn);
            jedis.mset(new String[]{"title","婴幼儿奶粉","num","2"});
            List<String> goods = jedis.mget(new String[]{"sn", "title", "num"});
            System.out.println(goods);
            Long num = jedis.incr("num");
            System.out.println(num);
            //Hash
            jedis.hset("student:3312","name","张晓明");
            String name = jedis.hget("student:3312", "name");
            System.out.println(name);
            Map<String,String> studentMap = new HashMap();
            studentMap.put("name","李兰");
            studentMap.put("age","18");
            studentMap.put("id","3313");
            jedis.hmset("student:3312",studentMap);
            Map<String, String> smap = jedis.hgetAll("student:3312");
            System.out.println(smap);
            //List
            jedis.del("letter");
            jedis.rpush("letter", new String[]{"d","e","f"});
            jedis.lpush("letter", new String[]{"c","b","a"});
            List<String> letter = jedis.lrange("letter", 0, -1);
            jedis.lpop("letter");
            jedis.rpop("letter");
            letter = jedis.lrange("letter", 0, -1);
            System.out.println(letter);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }
}

Jedis缓存String

package com.jesse;

public class Goods {
    private Integer goodsId;
    private String goodsName;
    private String description;
    private Float price;

    public Goods() {
    }

    public Goods(Integer goodsId, String goodsName, String description, Float price) {
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.description = description;
        this.price = price;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }
}
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>
package com.jesse;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CacheSample {
    public CacheSample(){
        Jedis jedis = new Jedis("192.168.1.1");
        try{
            List<Goods> goodsList = new ArrayList<Goods>();
            goodsList.add(new Goods(8818,"红富士苹果","",3.5f));
            goodsList.add(new Goods(8819,"进口脐橙","",5f));
            goodsList.add(new Goods(8820,"进口香蕉","",25f));
            jedis.auth("123");
            jedis.select(3);
            for (Goods goods : goodsList) {
                String json = JSON.toJSONString(goods);
                System.out.println(json);
                String key = "goods:" + goods.getGoodsId();
                jedis.set(key, json);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    public static void main(String[] args) {
        new CacheSample();
        System.out.printf("请输入要查询的商品编号:");
        String goodsId = new Scanner(System.in).next();
        Jedis jedis = new Jedis("192.168.1.1");
        try{
            jedis.auth("123");
            jedis.select(3);
            String key = "goods:" + goodsId;
            if (jedis.exists(key)){
                String json = jedis.get(key);
                System.out.println(json);
                Goods g = JSON.parseObject(json, Goods.class);
                System.out.println(g.getGoodsName());
                System.out.println(g.getPrice());
            }else{
                System.out.println("您输入的商品编号不存在,请重新输入!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }
}

参考

Redis入门

原文:https://www.cnblogs.com/jessekkk/p/12790585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!