Zhanhailiang Date: 2014-12-02
Related dependencies
1. Environment deployment
- Redis installation configuration tutorial and Phpredis extended installation test
- Redis Installation and PHP extensions
- The Phpredis module is installed under Windows (the Php_redis.dll in the current window environment is basically 2.1.3, and the redis.so version under Linux has reached 2.2.5, which may lead to different levels of support for some instruction sets)
2. Redis instruction Manual
- Php-redis Chinese help Manual. CHM (this manual is relatively old, please refer to Redis Commands)
- Redis Commands
- EVAL script Numkeys key [key ...] arg [arg ...]
3. Lua Language Basics
- "Lua 5.1 Reference Manual" cloud-inspired translation
- Application of LUA TABLE traversal array
4. How Redis communicates with LUA
- Lua: Getting Started with Redis users
- New Redis Branch for development of server-side LUA scripting support
- "Redis 2.6 Lua scripting feature Implementation Analysis"
Benefits of integrating LUA scripting operations with Redis
be able to get on redis faster! Many Redis applications are used in Read-compute-write mode, which allows a simple data calculation to require the client to communicate with the server two times, and if the intermediate compute process is transferred to the server, it can be multiplied to reduce the round-trip time.
make full use of cpu! The vast majority of Redis applications are IO-intensive, and even if they reach the cpu100% limit (Redis cannot use multicore), CPU usage is mostly handled on the network protocol stack, but if you use scripts executed by the server, you can make the Redis The CPU of the server is utilized.
But the fundamental reason is that we can only implement the most basic features in Redis that meet the needs of 99% of users, and leave 1% of the features in other unique scenarios to the custom server execution script. For those of you who fear that Redis will become redundant after the introduction of server-side scripting, you can breathe a sigh of relief as this is the ultimate solution to prevent the endless need to be met.
See the original: Redis new branch for development of server-side LUA scripting support
Application principle
The scheme for Redis to communicate with Lua scripts is to pass the Lua script as a command to the server through the client, read the script to the server, execute it after the interpreter is interpreted, and return to implement it.
It provides two interface implementations:
[Root@~/Wade/Lua/Historybrowsing]# REDIS-CLI127.0.0.1:6379> Help EvalEVAL Script Numkeys Key[Key ...]Arg[Arg ...]Summary:execute a Lua script server side since:2.6.0 group:scripting 127.0.0.1:6379> HelpEvalsha Evalsha SHA1 Numkeys key[Key ...]Arg[Arg ...]Summary:execute a Lua script server side since:2.6.0 group:scripting
Next, a simple demo implementation is used to explain the scenario:
A name2 data is stored on the Redis server with a storage structure of zset, which we can access in the following ways:
127.0.0.1:6379 > eval Span class= "St_h" style= "Color:rgb (255,0,0)" ">" return Redis.call ("Zrange", "Name2", 0,-1); ' 0 1 ) "1"
Of course, the above example is only to illustrate the use of Eval, the actual operation directly redis-client Zrang name2 0-1.
But if I ask for the ability to read the key dynamically to get the corresponding value, then I need to use LUA script to implement the corresponding function.
The following code, managed by github.com, implements the browsing history Add feature, and interested students can learn to do so without having to worry about the specific business logic, just to be concerned about how Redis communicates with Lua:
Source code See: Https://github.com/billfeller/historyBrowsing
Redis Integrated LUA scripting implementation