Redis:将值中的键引用扩展为它们的值。

我对 redis 很新颖。我有一个 CMS,它以以下结构的方式将 JSON 输出到 redis 中:

partial:1 => {id: 1, title: 'Foo1', ...}
partial:2 => {id: 2, title: 'Foo2', ...}
partial:3 => {id: 3, title: 'Foo3', ...}

page:home => {
   id: 'Homepage',
   teaser1: 'partial:1',
   teaser2: 'partial:2',
   teaser3: {type: Slider, content: 'partial:3'}
}

因此,JSON 可以包含符合其结构的特定命名方案的其他 redis-keys。我想要的是一种查询 redis 的方法,以便在获取 page:home-key 时,引用 json 中的其他键'展开'为其各自的值,如下所示:

{
   id: 'Homepage',
   teaser1: {id: 1, title: 'Foo1', ...},
   teaser2: {id: 2, title: 'Foo2', ...},
   teaser3: {type: Slider, content: {id: 3, title: 'Foo3', ...}
}

这是可能的,如何实现?

点赞
用户107090
用户107090

我不懂 Redis,但也许这对你有用:

local T=[[
partial:1 => {id: 1, title: 'Foo1', ...}
partial:2 => {id: 2, title: 'Foo2', ...}
partial:3 => {id: 3, title: 'Foo3', ...}

page:home => {
   id: 'Homepage',
   teaser1: 'partial:1',
   teaser2: 'partial:2',
   teaser3: {type: Slider, content: 'partial:3'}
}
]]

local D={}
for k,v in T:gmatch("(%w+:%w+)%s*=>%s*(%b{})") do
    D[k]=v
end

print((T:gsub("'(.-)'",D)))
2014-11-11 09:00:12
用户1058509
用户1058509

是的,这是完全可能的。下面是一个可用的方法:

  1. 创建一个将 Redis 哈希(例如:partial:*,page:home)转换为 Lua 表格的函数:**hgetall**。

  2. 创建一个检查给定的 Redis 哈希名称是否符合您的命名方案的函数。如果是,则使用 hgetall 将其转换为 Lua 表格;否则返回相同的值:**evaluate_hash**。

  3. 创建一个评估和转换给定哈希名称的所有属性,并将其作为 JSON 返回的函数:**expand**。

下面是一个简单的实现:

--Script: redis_expand_as_json.lua

-- 将给定的哈希名称(例如:partial:1..n,page:home)转换为 Lua 表格的函数。
local function hgetall(a)
  local b = redis.call('HGETALL', a)
  local c = {}
  local d
  for e, f in ipairs(b) do
    if e % 2 == 1 then
      d = f
    else
      c[d] = f
    end
  end
  return c
end

-- 检查给定值是否符合命名方案,
-- 如果是,则将其转换为 Lua 表格;否则将值返回。
local function evaluate_hash(value)
  local pattern = "partial:%d+"
  if string.match(value, pattern) then
    return hgetall(value)
  else
    return value
  end
end

-- 将给定的哈希名称转换为 Lua 表格,
-- 如果有必要,遍历所有元素并将其转换为 Lua 表格,
-- 并将表格作为 JSON 对象返回。
local function expand(hash_name)
  local obj_table = hgetall(hash_name)
  for k, val in pairs(obj_table) do
    obj_table[k] = evaluate_hash(val)
  end
  return cjson.encode(obj_table)
end

local page = KEYS[1]
local json_result = expand(page) or {}

redis.log(redis.LOG_NOTICE, tostring(json_result))
return json_result

在控制台中进行测试:

redis-cli -c hmset "partial:1" id 1 title foo1

OK

redis-cli -c hmset "partial:2" id 2 title foo2

OK

redis-cli -c hmset 'page:home' id 'Homepage' teaser1 'partial:1' teaser2 'partial:2'

OK

redis-cli EVAL "$(cat redis_expand_as_json.lua)" 1 'page:home'

{"teaser1":{"id":"1","title":"foo1"},"teaser2":{"id":"2","title":"foo2"},"id":"Homepage"}
2014-11-26 06:10:42