前言
本文主要給大家介紹了關(guān)于phpredis執(zhí)行LUA腳本的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧
示例代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
$lua = <<<EOT local kws = {} local lrkws = {} local nkws = {} local kw_ids = {} local lr_ids = {} local n_ids = {} for kw in string.gmatch(KEYS[1], "[^|]+" ) do table.insert(kws, "kw:" ..kw) end for kw in string.gmatch(KEYS[2], "[^|]+" ) do table.insert(lrkws, "lrkw:" ..kw) end for kw in string.gmatch(KEYS[3], "[^|]+" ) do table.insert(nkws, "nkw:" ..kw) end if #kws > 0 then kw_ids = redis.call( 'sinter' , unpack(kws)) end if #lrkws > 0 then lr_ids = redis.call( 'sinter' , unpack(lrkws)) end if #nkws > 0 then n_ids = redis.call( 'sinter' , unpack(nkws)) end local cache_key = ARGV[1] for _, v in ipairs(kw_ids) do redis.call( 'sadd' , cache_key, v) end for _, v in ipairs(lr_ids) do redis.call( 'sadd' , cache_key, v) end for _, v in ipairs(n_ids) do redis.call( 'sadd' , cache_key, v) end redis.call( 'expire' , cache_key, 600) return redis.call( 'scard' , cache_key) EOT; $ret = $redis -> eval ( $lua , array ( "你好|謝謝" , "" , "hello" , "cache_key" ), 3); echo $ret ; |
例子中傳入3個(gè)KEYS參數(shù),1個(gè)ARGV參數(shù)。 KEYS參數(shù)是字符串,單詞之間用 | 分割。
lua腳本最后將查詢(xún)結(jié)果存入 ARGV參數(shù)指定的key中,并返回結(jié)果set的成員個(gè)數(shù)。
需要注意的是, eval函數(shù)的第3個(gè)參數(shù)為KEYS個(gè)數(shù)。 phpredis依據(jù)此值將KEYS和ARGV做區(qū)分。
參考網(wǎng)頁(yè): https://github.com/phpredis/phpredis/blob/develop/tests/RedisTest.php
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://blog.csdn.net/jingtan/article/details/53392309