# @w5/redis_lua

> [‼️]: ✏️README.mdt

Latest version **0.0.14** (published 2023-06-26) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @w5/redis_lua
pnpm add @w5/redis_lua
yarn add @w5/redis_lua
bun add @w5/redis_lua
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.14 |
| Published | 2023-06-26 |
| First published | 2023-05-11 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | i@wac.tax |
| Maintainers | i-user |

## Links

- npm: https://www.npmjs.com/package/@w5/redis_lua
- Repository: https://github.com/wactax/node
- Homepage: https://github.com/wactax/node/tree/main/redis_lua
- Issues: https://github.com/wactax/node/issues
- npm.io page: https://npm.io/package/@w5/redis_lua

## Dependencies (3)

- [@w5/u8](https://npm.io/package/@w5/u8.md) ^0.0.1
- [@w5/dot](https://npm.io/package/@w5/dot.md) ^0.0.2
- [@w5/utf8](https://npm.io/package/@w5/utf8.md) ^0.0.3

## Recent versions

- 0.0.14 (latest) — 2023-06-26
- 0.0.13 — 2023-06-26
- 0.0.11 — 2023-06-26
- 0.0.10 — 2023-05-17
- 0.0.9 — 2023-05-11
- 0.0.8 — 2023-05-11
- 0.0.7 — 2023-05-11
- 0.0.6 — 2023-05-11
- 0.0.5 — 2023-05-11
- 0.0.2 — 2023-05-11
- 0.0.1 — 2023-05-11

## README

[‼️]: ✏️README.mdt

# @w5/redis_lua

[test/main.coffee](./test/main.coffee) :

```coffee
#!/usr/bin/env coffee

> @w5/uridir
  @w5/read
  path > join
  @w5/redis_lua
  @w5/redis_lua/dot_bind.js

ROOT = uridir(import.meta)
LUA = read join ROOT,'redis.lua'

redis = {
  fbinR:=>
    new Uint8Array([])
  fnload:(lua)=>
    console.log lua
    return
  fstr:(args...)=>
    console.log 'fstr', ...args
    return
}


await RedisLua(redis).RedisLuaTest LUA

console.log '---'

BIND = DotBind redis

BIND.fstr.ipLimit()

await redis.ipLimit('key1','key2')('arg1','arg2')
```

output :

```
#!lua name=RedisLuaTest

local log = function(...)
  local li = {}
  for _, v in ipairs({ ... }) do
    table.insert(li, cjson.encode(v))
  end
  redis.log(redis.LOG_NOTICE, unpack(li))
end

local _byZid = function(key, score)
  score = tonumber(score)
  local r = redis.call("ZRANGEBYSCORE", key, score, score, "LIMIT", 0, 1)
  if r then
    return r[1]
  end
end

local intBin = function(n)
  local t = {}
  while n > 0 do
    local r = math.fmod(n, 256)
    table.insert(t, string.char(r))
    n = (n - r) / 256
  end
  return table.concat(t)
end

local binInt = function(str)
  local n = 0
  local base = 1
  for i = 1, #str do
    local c = str:sub(i, i)
    n = n + base * c:byte()
    base = base * 256
  end
  return n
end

local HSET = function(key, field, val)
  return redis.call("HSET", key, field, val)
end

local HGET = function(key, field)
  return redis.call("HGET", key, field)
end

local HINCRBY = function(key, field, increment)
  return redis.call("HINCRBY", key, field, increment)
end

local INCR = function(key)
  return redis.call("INCR", key)
end

local ZSCORE = function(key, member)
  local r = redis.call("ZSCORE", key, member)
  if r then
    return r.double
  end
end

local ZADD_XX = function(key, member, score)
  return redis.call("ZADD", key, "XX", score, member)
end

local ZADD = function(key, member, score)
  return redis.call("ZADD", key, score, member)
end

local ZINCRBY = function(key, member, increment)
  return redis.call("ZINCRBY", key, increment, member).double
end

local SISMEMBER = function(key, member)
  return redis.call("SISMEMBER", key, member)
end

local EXPIRE = function(key, seconds)
  return redis.call("EXPIRE", key, seconds)
end

local TTL = function(key)
  return redis.call("TTL", key)
end

local _zid = function(kv, k)
  local id = ZSCORE(kv, k)
  if nil == id then
    id = -ZINCRBY(kv, "", -1)
    ZADD(kv, k, id)
  end
  return id
end

redis.register_function('zid',function(keys, args)
  redis.setresp(3)
  return _zid(keys[1], args[1])
end)

redis.register_function{
function_name='zmax',
callback=function(keys, args)
  redis.setresp(3)
  local zset = keys[1]
  local max = redis.call("ZRANGE", zset, 0, 0, "REV", "WITHSCORES")
  if next(max) ~= nil then
    local min = args[1]
    if min ~= nil then
      min = tonumber(min)
      local max = max[1]
      if max[2].double > min then
        return max[1]
      end
    end
  end
end,
flags={'no-writes'}
}

redis.register_function('zero',function(keys, args)
  redis.setresp(3)
  local key = keys[1]
  local t = redis.call("ZRANGEBYSCORE", key, "(0", "+inf")
  local r = {}
  for i = 1, #t do
    table.insert(r, 0)
    table.insert(r, t[i])
  end
  redis.call("ZADD", key, unpack(r))
end)

local _ztouch = function(func, keys, args)
  local zset = keys[1]
  local key = args[1]
  local max = redis.call("ZRANGE", zset, 0, 0, "REV", "WITHSCORES")
  if next(max) == nil then
    max = 1
  else
    max = max[1]
    local val = max[2].double
    if max[1] == key and val ~= 0 then
      return
    end
    max = val + 1
  end
  func(zset, key, max)
end

redis.register_function('ztouch',function(keys, args)
  redis.setresp(3)
  _ztouch(ZADD, keys, args)
end)

redis.register_function('ztouchXx',function(keys, args)
  redis.setresp(3)
  _ztouch(ZADD_XX, keys, args)
end)

redis.register_function{
function_name='RedisLuaTestVer',
callback=function()
  redis.setresp(3)
  return "\188\0\180\11\100\122\172\50\199\66\224\34\38\70\84\131\139\84\230\175\183\85\160\149\120\156\137\85\99\119\50\166"
end,
flags={'no-writes'}
}
---
fstr ipLimit [ 'key1', 'key2' ] [ 'arg1', 'arg2' ]
```

---
_Source: https://npm.io/package/@w5/redis_lua · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
