13 Video Tutorial Implementing 'Luarestylrucache' Based on Ffi

13 Video Tutorial- Implementing ’luarestylrucache’ Based on FFI #

13 [Video] Practical: lua-resty-lrucache implemented based on FFI

Hello, I am Wen Ming.

Today’s content will also be presented in the form of a video. However, before you start watching the video, I would like to ask you a few questions:

  • What is the most important data structure in lua-resty-lrucache?
  • lua-resty-lrucache has two FFI implementations. Which one that we will discuss today is more suitable for what scenario?

These questions are also the core content that will be addressed in today’s video lesson. I hope you can take some time to think about them and come to the video with these questions in mind.

At the same time, I will provide corresponding written explanations to facilitate your summary and review after watching the video. Below is the written introduction for today’s lesson.

Today’s Core #

lua-resty-lrucache is an LRU cache library implemented using LuaJIT FFI, which can cache various types of data in the worker. The functionality is similar to shared dict, but shared dict can only store data of string type. In most practical cases, these two caches are used together - lrucache as the first-level cache and shared dict as the second-level cache.

The implementation of lrucache does not involve OpenResty’s Lua API. So even if you haven’t used OpenResty before, you can learn how to use LuaJIT’s FFI through this project.

The lrucache repository contains two implementation solutions - one uses Lua table to implement the cache, and the other uses a hash table. The former is more suitable for high hit rate cases, while the latter is suitable for low hit rate cases. There is no better solution between the two, it depends on which one is more suitable for your production environment.

Through today’s project, you can learn how to use FFI and understand what essential content should be included in a complete lua-resty library. Of course, I will also briefly introduce the use of travis.

Finally, I want to emphasize that when you are faced with an unfamiliar open source project, the documentation and test cases are always the best way to get started. And if you want to read the source code later, don’t focus on the details first, but take a look at the main data structures first, and gradually delve into the details around them.

Courseware Reference #

Today’s courseware has been uploaded to my GitHub. You can download it and study on your own.

Here is the link: https://github.com/iresty/geektime-slides

If you have any questions, you can ask in the comments section. You can also share your learning experience in the comments section. I look forward to our conversation and welcome you to share this article with your colleagues and friends. Let’s communicate and improve together.