2014-11-11 3 views
0

저는 redis를 처음 접했습니다. 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은 구조의 특정 명명 규칙을 준수 다른 레디 스 - 키를 포함 할 수 있습니다 :이 같은 구조 레디 스에 JSON을 출력하는 CMS가 있습니다.

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

이인가 :이처럼 JSON에서 다른 키에 대한 참조 - 키 page:home는 해당 값으로 '확대'얻을 얻을 때 있도록 내가하고 싶은 것은 레디 스를 조회하는 방법입니다 가능하고 어떻게 달성 될 수 있습니까?

+0

어떻게 루아이 관련이 – lhf

+0

레디 스 내가 그것을이 문제를 해결하기 위해 가능한 방법이 될 줄 알았는데 그 평가 방법에 루아 스크립트를 실행할 수 있듯이?. – jfd

답변

2

예. 그것은 아주 가능합니다. 테이블을 루아 (가정 : 부분 : * 페이지의 예) :

  1. 이 레디 스 해시를 변환하는 함수를 만듭니다 : hgetall

  2. 확인하는 함수를 만들고 여기에 작동하는 접근 방식 주어진 redis hashname이 당신의 명명 스키마와 일치한다면 hgetall을 사용하여 루아 테이블로 변환하십시오; evaluate_hash

  3. 평가하고 주어진 해시 이름의 모든 속성을 변환하고 JSON으로 반환하는 함수를 작성하십시오, 그렇지 않으면 같은 값을 반환

을 확장 다음은 간단한있어 구현 :

--script : redis_expand_as_json.lua

--Function to Convert a given hash name (e.g: partial:1..n, page:home) to a lua table 
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 

--Function to check if the given value conforms with a naming scheme, 
-- if so convert it to a lua table; otherwise return the values as is. 
local function evaluate_hash(value) 
    local pattern = "partial:%d+" 
    if string.match(value, pattern) then   
    return hgetall(value) 
    else 
    return value 
    end 
end 

--Function to convert a given hash_name to a lua table, 
-- iterate all elements and convert them to lua table if necessary 
-- returns the table as a json object 
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 
0 콘솔

시험 :

레디 스 -c-CLI hmset "부분 1"ID 1 foo1은

OK 표제

레디 스 -c-CLI hmset "부분 : 2 "id2 title foo2

OK

,

레디 스-CLI -c hmset '페이지 : 집'ID '홈페이지'teaser1 '부분 : 1'의 teaser2 '부분 : 2'

OK

레디 스-CLI EVAL "$ (고양이 redis_expand_as_json.루아) "1 '페이지 : 홈'

{"teaser1":{"id":"1","title":"foo1"},"teaser2":{"id":"2","title":"foo2"},"id":"Homepage"} 
0

내가 레디 스 모르지만 아마이 당신을 위해 작동합니다

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)))