Kai Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 六角鼠年鐵人賽 Week 24 - Spring Boot - Redis 特色功能介紹 ==大家好,我是 "為了拿到金角獎盃而努力著" 的文毅青年 - Kai== ## Elon Musk :::info When something is important enough, you do it even if the odds are not in your favor. ::: ## 主題 這週的文章算是要把 Redis 其他一些特色功能給做一個整理,接下來就要去寫其他文章了~ 在前幾週介紹了 Redis 的基本、資料結構、以及 Pub/Sub 功能外,其實 Redis 還支援了許多諸如 HyperLogLog 處理、腳本執行等服務,幫助開發者省下額外的開發時程。 ## HyperLogLog 服務 如果不知道什麼是 HyperLogLog 的人可以先看看這篇文章 2019-09-12 由 xy的技術圈 發表于程式開發 [一篇文章學懂Redis HyperLogLog](https://kknews.cc/zh-tw/code/2yggq9z.html) 簡單來說,開發人員常會需要去計算一些固定增加的數值資料,例如: 登入人次、瀏覽人次等等,不管事運用 Set 或 bitmaps 這種資料結構去儲存基數記數的資料都會累積的越來越肥大造成讀取效益越來越低落的情況,為此 HyperLogLog 演算法的應用,讓開發人員可以在一定的誤差條件內,**高效**的取得**近似值**的結果。 注意是**高效**和**近似值**! 相信從業工程師的人應該不難理解,要在時間、成本、範疇取得品質上的平衡是非常非常難以拿捏的事情。因此 HyperLogLog 的方式雖然存在一定誤差,但也提供了一個無疑是最佳解的方式。尤其以 Redis 只需要12K的內存,在標準差0.81%的前提下,能夠統計2^64個數據的情況來說,找不到更高效的替代方式了。 ![](https://epaper.geo.com.tw/ImageUpLoad/ShowImage?dXJsJTNkYUhSMGNITWxNMkVsTW1ZbE1tWmxjR0Z3WlhJdVoyVnZMbU52YlM1MGR5VXlaa2x0WVdkbGN5VXlabFZ3Ykc5aFpFbHRZV2RsSlRKbU1USTJKVEptY0RFdWFuQm4lMjZsaXN0U2VxJTNkMTE5ODY5NSUyNmNvbnRlbnRzU2VxJTNkMjk3OSUyNmNvbmZpZ1NlcSUzZDIzMQ==) 稍微介紹過後,來看看以下 Redis 提供 HyperLogLog 的操作指令: | 指令 | 描述 | 備註 | | ---- | ---- | ---- | | PFADD [key_name] [element_1] [element_2] ... | 將指定的 Element 加入指定 Key 的 HyperLogLog 當中 || | PFCOUNT [key_name1] [key_name2] ... | 將計算指定的 Keys 並返為統計記數 | 若 key 不存在則返回 0 | | PFMERGE [destKey] [sourceKey1] [sourceKey2] ... | 將指定的 SourceKeys 通通合為一個 DestKey || ## Transaction 事務處理 Redis 雖然是 NoSQL,但對於 Transaction 的部分也提供了一些服務,讓開發人員在操作 Redis 時,可以類似於 RDBMS 的方式比較順手,針對資料的部分處理也比較方便。 但 Redis 並不支持 Roll-Back 功能,因此在操作 Transaction 的時候,務必在處理前就已經把資料的部份給偵錯和校正完成,避免髒資料進入後難以回溯。 以下就是 Redis 提供的 Transaction 操作指令: | 指令 | 描述 | 備註 | | ---- | ---- | ---- | | MULTI | 標記 Transaction 的開始,後續所有的操作指令都會被寫入 QUEUED 中 | 永遠會回傳 1,後續操作指令會回傳 QUEUED | | EXEC | 處理所有儲存在 QUEUED 中的操作 || | DISCARD | 清空所有儲存在 QUEUED 中的操作 | 需要在 MULTI 之後才能下此指令,否則會返回無法找到 MULTI 的錯誤訊息| | WATCH [key_name1] [key_name2] ... | 針對指定 Keys 進行監視,當 Keys 在被 Watch 後有被任何 Client 端修正的狀況,則 Server 端的 Transaction 將不會執行,直接返回失敗 || | UNWATCH | 取消所有 Keys 的監視 || ## Lua 腳本處理 Redis 在 2.6 版本內嵌支持 Lua scripts 的功能,解決無法高效率的處理 CAS (Check-And-Set ) 命令的缺點。 什麼是 Lua? Lua 是一種輕量級的腳本語言,是用 C 語言編寫而成,被設計出來的目的是為了嵌入應用程式當中,從而替應用程式提供靈活的腳本應用範疇。 Lua 支持了 Procedure-Oriented 和 Functional Programming 的開發方式,並有自動內存管理的功能,支援處理數值、Hash、Collection、Object 的資料結構。 一些開發人員常用的 OOP 機制也有被設計在 Lua 中: 抽象、介面、繼承、重載等。 目前 Lua 大多嵌入在遊戲開發軟體、Web App、資料庫 Plugin 等方面。 Redis 目前提供關於 Lua 的操作指令如下: | 指令 | 描述 | 備註 | | ---- | ---- | ---- | | EVAL [scripts] [number_keys] [key_name1] [key_name2] ... [arg...] | 使用 EVAL 執行 Lua scripts 指令,並限定 number_keys 數量的 key_name1,key_name2 等 | | EVALSHA [sha1_scripts] [number_keys] [key_name1] [key_name2] ... [arg...] | 當一個 EVAL 的指令成功後,它會產出一組 SHA1 的資料,隨後針對這個 SHA1 使用 EVALSHA 的指令皆會得到一樣的結果,可以視為一個 scripts 在操作後會得到一組專屬的ID,使用這 ID 等同於使用該 scripts || | SCRIPTS EXISTS [scripts] | 查詢 scripts 是否存在於緩存中 || | SCRIPTS FLUSH | 刪除所有緩存中的 scripts || | SCRIPT KILL | 刪除目前正在執行中的腳本 || | SCRIPT LOAD [scripts] | 載入指定的 scripts 到緩存中 || ## 其他的操作指令 剩餘的功能比較零散,但還是有重要的指令可以學學 | 指令 | 描述 | 備註 | | ---- | ---- | ---- | | SAVE | 創建一個備份當前 DB 的 dump.rdb 文件在 Redis 目錄中 | ※此為關於 Redis 資料持久化處理的部分,後續文章會提| | BGSAVE | 在背景執行 SAVE 功能 | ※此為關於 Redis 資料持久化處理的部分,後續文章會提| | PING | 檢查 Server 端是否正在運行 | 會回傳 PONG | | QUIT | 關閉當前連接 || | SELECT [index] | 更改當前要連接的資料庫 | | ECHO [message] | 讓 Redis print 出 message || ## 結語 :::danger 這週的文章很短,也算是盡快把 Redis 剩餘的功能給介紹完了。 接下來幾週要開始準備學學 Activity 這個看起來對於開發上很好用的框架了。 若有斷文請見諒,學學新東西然後整理給大家知道真的不是那麼容易,加上上班偶爾會碰到需要加班處理的狀況,有時候有 這幾週開始分享 Spring Data & JPA 的教學 [六角鼠年鐵人賽 Week 25 - Spring Boot - Spring Data & JPA 介紹](/J_SpvD2ASN61ZaY-SCPSxA) ::: 首頁 [Kai 個人技術 Hackmd](/2G-RoB0QTrKzkftH2uLueA) ###### tags: `Redis`,`w3HexSchool`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully