大数据之路Week10_day07 (JavaAPI 操作Redis 模拟将redis当作缓存,从Mysql数据库中查询数据)

在现实生活中,当很多人去访问一个数据的时候,Mysql会很慢,甚至会挂掉,如果这里之间存在一个缓存的话,直接从内存中查询数据将会快很多。

这里就去模拟将redis看作是一个缓存,因为redis就是基于内存的数据库。

需要考虑的问题:

  1、客户端发起请求的时候,先去缓存中查询。

  2、如何设计redis中的Key-Value中的value的类型。

  3、怎么将将它看作缓存?设定过期时间,在缓存中查询到数据的情况下,将过期时间重置。

  4、如果在缓存中查询不到的情况下,去数据库中查询

  5、再将数据库查询到数据,添加到缓存中,并设定过期时间。

jedis.expire(key,60);   设置过期时间

package com.wyh.redis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.sql.*;

/**
 * 模拟redis是缓存
 *
 *
 */
public class RedisAsCache {
    private Connection conn;
    private Jedis jedis;
    private weibo weibo;


    @Before
    public void Cli(){

        try {
            //获取到数据库的连接信息
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
                    "root", "root");

            //获取到redis的连接
            jedis = new Jedis("master", 6379);

            System.out.println("成功连接到数据库。。"+conn);
            System.out.println("成功连接到redis。。。"+jedis);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取数据
     * 1、先去缓存(redis)中读取
     * 2、若缓存中存在数据(redis),就直接返回数据,并更新过期时间
     * 3、若缓存(redis)中没有,就去数据库(Mysql)中读取数据
     * 4、若查询到数据,将结果缓存到redis中,并设置过期时间
     * 5、返回结果
     */
    @Test
    public void readData(){
        long start = System.currentTimeMillis();

        int id = 2;
        //先读取缓存

        String tableName = "weibo:";

        String key = tableName+id;

        String result = jedis.get(key);

        if(result!=null){
            //更新过期时间
            jedis.expire(key,60);
            weibo = new weibo(id, result);

            System.out.println("缓存中查询到热门微博: "+weibo.getContext());
        }else{

            String sql = "select * from weibo where id=?";
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setInt(1,id);

                ResultSet resultSet = ps.executeQuery();

                if(resultSet.next()){
                    int id1 = resultSet.getInt("id");
                    String context = resultSet.getString("context");

                    weibo = new weibo(id1,context);
                    System.out.println("数据库查询到热门微博: "+weibo.getContext());
                }

                jedis.set(key,weibo.getContext());
                jedis.expire(key,60);


            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }



    @After
    public void close(){
        if(jedis!=null){
            jedis.close();
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

}
 
原文地址:https://www.cnblogs.com/wyh-study/p/12182318.html