# 利用Jedis缓存数据

# 导航

回到redis导航页

package com.torey.jedis;

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.5.49",6379);
        try {
            jedis.auth("root123");
            List<TGoodsEntity> tGoodsEntities = new ArrayList<TGoodsEntity>();
            tGoodsEntities.add(new TGoodsEntity(100,"title标题100","subTitle副标题100",5f));
            tGoodsEntities.add(new TGoodsEntity(101,"title标题101","subTitle副标题101",6f));
            tGoodsEntities.add(new TGoodsEntity(102,"title标题102","subTitle副标题102",7f));
            jedis.select(2);
            for (TGoodsEntity tGoodsEntity : tGoodsEntities) {
                String s = JSON.toJSONString(tGoodsEntity);
                String key="goods:"+tGoodsEntity.getGoodsId();
                System.out.println(s);
                jedis.set(key,s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }

    public static void main(String[] args) {
       // new CacheSample();
        System.out.println("请输入要检索的商品编号:");
        Jedis jedis = new Jedis("192.168.5.49",6379);
        jedis.auth("root123");
        jedis.select(2);
        try {
        String next = new Scanner(System.in).next();
        while (next!="exit"){
            String key="goods:"+next;
            Boolean exists = jedis.exists(key);
            if (exists) {
                String s = jedis.get(key);
                System.out.println(s);
            }else {
                System.out.println("您输入的商品编号不存在");
            }
            System.out.println("请输入要检索的商品编号:");
            next= new Scanner(System.in).next();
        }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

# 导航,上一页,下一页

5java中使用redis

# 支持我-微信扫一扫-加入微信公众号

Aseven公众号

# 赞赏作者

赞赏作者