
- Lua Tutorial
- Lua - Home
- Lua Basics
- Lua - Overview
- Lua - Environment
- Lua - Basic Syntax
- Lua - Comments
- Lua - Print Hello World
- Lua - Variables
- Lua - Data Types
- Lua - Operators
- Lua - Loops
- Lua - Generic For
- Lua - Decision Making
- Lua - Date and Time
- Lua Functions
- Lua - Functions
- Lua - Multiple Results
- Lua - Named Arguments
- Lua - Default/Optional Arguments
- Lua - Closures
- Lua - Uses of Closures
- Lua - Local Functions
- Lua - Anonymous Functions
- Lua - Functions in Table
- Lua - Proper Tail Calls
- Lua Strings
- Lua - Strings
- Lua - String Concatenation
- Lua - Loop Through String
- Lua - String to Int
- Lua - Split String
- Lua - Check String is NULL
- Lua Arrays
- Lua - Arrays
- Lua - Multi-dimensional Arrays
- Lua - Array Length
- Lua - Iterating Over Arrays
- Lua - Slicing Arrays
- Lua - Sorting Arrays
- Lua - Merging Arrays
- Lua - Sparse Arrays
- Lua - Searching Arrays
- Lua - Resizing Arrays
- Lua - Array to String Conversion
- Lua - Array as Stack
- Lua - Array as Queue
- Lua - Array with Metatables
- Lua - Immutable Arrays
- Lua - Shuffling Arrays
- Lua Iterators
- Lua - Iterators
- Lua - Stateless Iterators
- Lua - Stateful Iterators
- Lua - Built-in Iterators
- Lua - Custom Iterators
- Lua - Iterator Closures
- Lua - Infinite Iterators
- Lua - File Iterators
- Lua - Table Iterators
- Lua - Numeric Iterators
- Lua - Reverse Iterators
- Lua - Filter Iterators
- Lua - Range Iterators
- Lua - Chaining Iterators
- Lua Tables
- Lua - Tables
- Lua - Tables as Arrays
- Lua - Tables as Dictionaries
- Lua - Tables as Sets
- Lua - Table Length
- Lua - Table Iteration
- Lua - Table Constructors
- Lua - Loop through Table
- Lua - Merge Tables
- Lua - Nested Tables
- Lua - Accessing Table Fields
- Lua - Copy Table by Value
- Lua - Get Entries from Table
- Lua - Table Metatables
- Lua - Tables as Objects
- Lua - Table Inheritance
- Lua - Table Cloning
- Lua - Table Sorting
- Lua - Table Searching
- Lua - Table Serialization
- Lua - Weak Tables
- Lua - Table Memory Management
- Lua - Tables as Stacks
- Lua - Tables as Queues
- Lua - Sparse Tables
- Lua Lists
- Lua - Lists
- Lua - Inserting Elements into Lists
- Lua - Removing Elements from Lists
- Lua - Iterating Over Lists
- Lua - Reverse Iterating Over Lists
- Lua - Accessing List Elements
- Lua - Modifying List Elements
- Lua - List Length
- Lua - Concatenate Lists
- Lua - Slicing Lists
- Lua - Sorting Lists
- Lua - Reversing Lists
- Lua - Searching in Lists
- Lua - Shuffling List
- Lua - Multi-dimensional Lists
- Lua - Sparse Lists
- Lua - Lists as Stacks
- Lua - Lists as Queues
- Lua - Functional Operations on Lists
- Lua - Immutable Lists
- Lua - List Serialization
- Lua - Metatables with Lists
- Lua Modules
- Lua - Modules
- Lua - Returning Functions from Modules
- Lua - Returning Functions Table from Modules
- Lua - Module Scope
- Lua - SubModule
- Lua - Module Caching
- Lua - Custom Module Loaders
- Lua - Namespaces
- Lua - Singleton Modules
- Lua - Sharing State Between Modules
- Lua - Module Versioning
- Lua Metatables
- Lua - Metatables
- Lua - Chaining Metatables
- Lua Coroutines
- Lua - Coroutines
- Lua File Handling
- Lua - File I/O
- Lua - Opening Files
- Lua - Modes for File Access
- Lua - Reading Files
- Lua - Writing Files
- Lua - Closing Files
- Lua - Renaming Files
- Lua - Deleting Files
- Lua - File Buffers and Flushing
- Lua - Reading Files Line by Line
- Lua - Binary File Handling
- Lua - File Positioning
- Lua - Appending to Files
- Lua - Error Handling in File Operations
- Lua - Checking if File exists
- Lua - Checking if File is Readable
- Lua - Checking if File is Writable
- Lua - Checking if File is ReadOnly
- Lua - File Descriptors
- Lua - Creating Temporary Files
- Lua - Working with Large Files
- Lua Advanced
- Lua - Error Handling
- Lua - Debugging
- Lua - Garbage Collection
- Lua - Object Oriented
- Lua - Web Programming
- Lua - Database Access
- Lua - Game Programing
- Lua Useful Resources
- Lua - Quick Guide
- Lua - Useful Resources
- Lua - Discussion
Lua - Module Caching
Module caching refers to keeping a copy of module loaded and checking if it is already loaded before reloading the module. Lua keeps track of each module loaded using require statement. Lua maintains a global table package.loaded and caches the loaded module in this table.
Caching Mechanism
Following section showcases how Lua caches modules.
First require call
-- load the module stringutils = require("stringutils")
When a require() statement is encountered for the first time, Lua performs following actions −
Lua searches for the file name stringutils.lua in the current path or path defined by LUA_PATH environment variable.
Once a file is found, its content are loaded into the memory and are executed.
The table returned by the module or any other entity is then stored in Lua's internal cache associated with key as module name passed. In this case it is stringutils.
The returned table/value by the module is assigned to a variable stringutils.
Subsequent require call
-- load the module again stringutils1 = require("stringutils")
When a require() statement is encountered again with same module name, Lua checks its cache first −
Lua searches for the module name stringutils in its cache, as module is found, the same is returned.
The returned cached table/value by the module is assigned to a variable stringutils.
Example - Showcasing Module Caching
Create a Module which executes a message when loaded.
counter_module.lua
print("Counter module is loaded and executed.") local counter = 0 local counterModule = {} function counterModule.increment() -- increment the counter counter = counter + 1 -- print the counter print("Counter:", counter) end return counterModule
Create Main Module and load the counter module twice.
moduletutorial.lua
-- load the counter module for the first time -- prints Counter module is loaded and executed. local counterModule1 = require("counter_module") -- call the increment() function to print the counter -- prints Counter: 1 counterModule1.increment() -- load the counter module again but it will not print the loaded messaage -- owing to module caching local counterModule2 = require("counter_module") -- call the increment() function to print the counter -- prints Counter: 2, as cached module counter was incremented initially counterModule2.increment() -- load the counter module again but it will not print the loaded messaage -- owing to module caching local counterModule3 = require("counter_module") -- call the increment() function to print the counter -- prints Counter: 3, as cached module counter was -- incremented again in previous statement counterModule3.increment() -- Output: Counter: 3 -- compare modules, each comparison prints true print(counterModule1 == counterModule2) print(counterModule2 == counterModule3)
Output
In order to run this code, we need to place the two Lua files in the same directory or alternatively, you can place the module file in the package path and it needs additional setup. When we run the above program, we will get the following output−
Counter module is loaded and executed. Counter: 1 Counter: 2 Counter: 3 true true
Explanation
counter_module.lua
As first line, we're having a print statement which will be printed when the module is loaded.
We've created a variable counter and initialized it to zero and an empty table counterModule as a module.
As next, we've defined a function increment() which increments the counter and prints the updated counter.
In the end, we're returning the counterModule table.
moduletutorial.lua
local counterModule1 = require("counter_module") statement is used to load the module for the first time and assign to counterModule1 variable and a message "Counter module is loaded and executed." is printed.
counterModule1.increment() is used to increment module counter and print it.
local counterModule2 = require("counter_module") tries to reload the same module, but it comes as a cached copy as the loaded message is not getting printed.
counterModule2.increment() increments the counter of cached module and prints the same.
Finally, we're comparing print(counterModule1 == counterModule2). Being same module internally, comparison returns true.
Advantages of Module Caching
Improved Performance − As module is loaded only once, we can avoid file I/O multiple times and faster program startup.
Ensures Singletons − In case module is having a singleton then caching will help in maintaining singleton.
Initialization − As a module is loaded only once, the initialization block will be called once and can be used to initialize variables of the module.