• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Shopify/wolverine: Scripting redis with lua made even easier

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Shopify/wolverine

开源软件地址(OpenSource Url):

https://github.com/Shopify/wolverine

开源编程语言(OpenSource Language):

Ruby 98.9%

开源软件介绍(OpenSource Introduction):

Wolverine Dependency Status Build Status

Wolverine is a simple library to allow you to manage and run redis server-side lua scripts from a rails app, or other ruby code.

Redis versions 2.6 and up allow lua scripts to be run on the server that execute atomically and very quickly.

Wolverine is a wrapper around that functionality, to package it up in a format more familiar to a Rails codebase.

How do I use it?

  1. Make sure you have redis 2.6 or higher installed.
redis-server -v
  1. Add wolverine to your Gemfile:
gem 'wolverine'
  1. Add your lua scripts to app/wolverine:
-- app/wolverine/util/mexists.lua
local exists = {}
local existence
for _, key in ipairs(KEYS) do
  table.insert(exists, redis.call('exists', key))
end
return exists
  1. Call wolverine from your code:
Wolverine.util.mexists(['key1', 'key2', 'key3']) #=> [0, 1, 0]

Or

Wolverine.util.mexists(:keys => ['key1', 'key2', 'key3']) #=> [0, 1, 0]

Methods are available on Wolverine paralleling the directory structure of wolverine's script_path.

Nested Lua Scripts

For lua scripts with shared code, Wolverine supports ERB style templating.

If your app has lua scripts at

  • app/wolverine/do_something.lua
  • app/wolverine/do_something_else.lua

that both have shared lua code, you can factor it out into a lua "partial":

  • app/wolverine/shared/_common.lua
-- app/wolverine/shared/_common.lua
local function complex_redis_command(key, value)
  local dict = {}
  dict[key] = value
end
-- app/wolverine/do_something.lua
<%= include_partial 'shared/_common.lua' %>
complex_redis_command("foo", "bar")
return true
-- app/wolverine/do_something_else.lua
<%= include_partial 'shared/_common.lua' %>
complex_redis_command("bar", "baz")
return false
  • partials are loaded relative to the Wolverine.config.script_path location
  • partials can be protected by adding an underscore (eg. shared/_common.lua). This disallows EVAL access through Wolverine.shared._common

Configuration

Available configuration options:

  • Wolverine.config.redis (default Redis.new)
  • Wolverine.config.script_path (default Rails.root + 'app/wolverine')
  • Wolverine.config.instrumentation (default none)

If you want to override one or more of these, doing so in an initializer is recommended but not required. See the full documentation for more details.

More information

For more information on scripting redis with lua, refer to redis' excellent documentation: http://redis.io/commands/eval




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap