StackExchange. Redis loads the Lua script for batch deletion and modification of fuzzy queries. stackexchange. redis

Source: Internet
Author: User

StackExchange. Redis loads the Lua script for batch deletion and modification of fuzzy queries. stackexchange. redis
Preface

Use StackExchange. redis does not directly use related methods to batch delete or modify Fuzzy queries. Although you can use Scan-related methods to perform fuzzy queries, for example, HashScan ("hashkey ", "* key *"), and then use the relevant methods for batch operations, but if the cache data volume is large, the efficiency is low, you can use the Lua script to Perform Batch operations on Fuzzy queries: ScriptEvaluate (luasate. prepare (...)).

 

Batch delete operations after fuzzy search using keys
1 var redis = ConnectionMultiplexer. connect ("Maid: 6379, allowAdmin = true"); 2 redis. getDatabase (). scriptEvaluate (luasate. prepare (3 // Redis keys fuzzy query: 4 "local ks = redis. call ('keys ', @ keypattern) "+ // local ks defines a local variable, which is used to store the obtained KEYS 5" for I = 1, # ks, 5000 do "+ // # ks indicates the number of ks sets. The statement means: for (int I = 1; I <= ks. count; I + = 5000) 6 "redis. call ('del ', unpack (ks, I, math. min (I + 4999, # ks) "+ // the index value of the Lua set starts from 1, and unpack is used to unpack the data in the ks set. Each time 5000, then delete 7 "end" + 8 "return true" 9), 10 new {keypattern = "mykey *"});
Batch Modification
1             redis.GetDatabase().ScriptEvaluate(LuaScript.Prepare(2                 " local ks = redis.call('KEYS', @keypattern) " + 3                 " for i=1,#ks do " +    4                 "     redis.call('set', ks[i], @value) " +5                 " end " +6                 " return true "),7                 new { keypattern = "mykey*", value = "setval" });
Batch deletion of keys in a Hash set after fuzzy query
1 redis. getDatabase (). scriptEvaluate (luasate. prepare (2 "local ks = redis. call ('hkeys ', @ hashid) "+ 3" local fkeys = {} "+ 4" for I = 1, # ks do "+ 5 // use string. find to perform the matching operation 6 "if string. find (ks [I], @ keypattern) then "+ 7" fkeys [# fkeys + 1] = ks [I] "+ 8" end "+ 9" end "+ 10" for I = 1, # fkeys, 5000 do "+ 11" redis. call ('hdel ', @ hashid, unpack (fkeys, I, math. min (I + 4999, # fkeys) "+ 12" end "+ 13" return true "14), 15 new {hashid =" hkey ", keypattern = "^ mykey"}); // The keypattern can be a regular expression.
Batch Modification
1 redis. getDatabase (). scriptEvaluate (luasate. prepare (2 "local ks = redis. call ('hkeys ', @ hashid) "+ 3" local fkeys = {} "+ 4" for I = 1, # ks do "+ 5" if string. find (ks [I], @ keypattern) then "+ 6" fkeys [# fkeys + 1] = ks [I] "+ 7" end "+ 8" end "+ 9" for I = 1, # fkeys do "+ 10" redis. call ('hset', @ hashid, fkeys [I], @ value) "+ 11" end "+ 12" return true "13 ), 14 new {hashid = "hkey", keypattern = "^ key", value = "hashValue"}); // keypattern can be a regular expression
Batch delete operations after fuzzy query of values in the Set
1 redis. getDatabase (). scriptEvaluate (luasate. prepare (2 "local ks = redis. call ('smembers ', @ keyid) "+ 3" local fkeys = {} "+ 4" for I = 1, # ks do "+ 5" if string. find (ks [I], @ keypattern) then "+ 6" fkeys [# fkeys + 1] = ks [I] "+ 7" end "+ 8" end "+ 9" for I = 1, # fkeys, 5000 do "+ 10" redis. call ('srem', @ keyid, unpack (fkeys, I, math. min (I + 4999, # fkeys) "+ 11" end "+ 12" return true "13), 14 new {keyid =" setkey ", keypattern = "^ myval"}); // The keypattern can be a regular expression.
Note:

The Lua script can be evaluated using the built-in Lua interpreter using the EVAL command starting from Redis 2.6.0.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.