【非關係型資料庫NOSQL Redis】 NoSql與Redis 關係型資料庫和非關係型資料庫的區別 Redis的介紹 Redis的應用場景

2020-10-25 15:01:04

NOSQL介紹

(1)什麼是NOSQL
NoSQL(NoSQL = Not Only SQL),意即「不僅僅是SQL」,是一項全新的資料庫理念,泛指非關係型的資料庫
NOSQL是非關係型資料庫
Redis: 就是NOSQL 非關係型資料庫
MySql Oracle :關係型資料庫

關係型資料庫和非關係型資料庫的區別

關係型資料庫(Mysql Oracle SqlServer)

1:資料是由一張張的表組成,而且這些表與表之間有關係(一對一,一對多,多對多)
2:資料是存在硬碟上,每次存取時,是將資料從硬碟讀取到記憶體中
在這裡插入圖片描述

非關係型資料庫(NOSQL)

》1: 資料是有一個個的鍵值對:鍵 值
》2:資料是存在記憶體中,在滿足需要的時候,也可以將資料存在硬碟上(Redis的持久化)
在這裡插入圖片描述

Redis的介紹

Redis(Remote Dictionary Server ) 遠端字典服務,是一個非關係型資料庫

使用Redis的好處

每一次頁面載入後都會重新請求資料庫來載入,對資料庫的壓力比較大,而且分類的資料不會經常產生變化,所有可以使用redis來快取這個資料。
在這裡插入圖片描述

應用場景

Redis一般用來儲存經常存取的,但有不經常改變的資料

  1. 快取 處理一些臨時資料
  2. 聊天室的線上好友列表
  3. 網站存取統計

Redis使用

雖然 存取redis 較快,但是第一次是沒有資料的
存取service,獲取json,將json儲存到redis

redis伺服器

開啟start.bat,開啟伺服器
在這裡插入圖片描述

開啟redis圖形化工具

在這裡插入圖片描述

JedisUtil工具讀寫資料

package com.wzx.lvyou.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Jedis工具類
 */
public final class JedisUtil {
    private static JedisPool jedisPool;

    static {
        //讀取組態檔
        InputStream is = JedisPool.class.getClassLoader().getResourceAsStream("jedis.properties");
        //建立Properties物件
        Properties pro = new Properties();
        //關聯檔案
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //獲取資料,設定到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

        //初始化JedisPool
        jedisPool = new JedisPool(config, pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));


    }


    /**
     * 獲取連線方法
     */
    public static Jedis getJedis() {
        return jedisPool.getResource();
    }

    /**
     * 關閉Jedis
     */
    public static void close(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }
}

jedis.properties

host=192.168.21.101
port=6379
maxTotal=100  //最大連線數
maxIdle=10    //最大空閒數

CategoryServlet使用redis

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      
        //存取service,獲取json,將json儲存到redis
        Jedis jedis = JedisUtil.getJedis();
        String json = jedis.get("category_list");

        if (json != null) {
            System.out.println("redis cache");
            response.getWriter().println(json);

        } else {   //存取redis 較快,但是第一次是沒有資料的,所以存取資料庫
            System.out.println("mysql data");
            //建立業務物件
            CategoryService categoryService = new CategoryService();
            //所有的分類
            List<Category> categoryList=categoryService.findAll();
            //顯示
            ResponseInfo info = new ResponseInfo();
            info.setCode(200);
            info.setData(categoryList);
            json = new ObjectMapper().writeValueAsString(info);
            //將資料儲存到redis
            jedis.set("category_list",json);
            response.getWriter().println(json);
        }
        //關閉連線
        JedisUtil.close(jedis);

    }