2014-04-28 2 views

답변

3

방법은 다음과 같습니다

# Expands out the +key+ argument into a key that can be used for the 
    # cache store. Optionally accepts a namespace, and all keys will be 
    # scoped within that namespace. 
    # 
    # If the +key+ argument provided is an array, or responds to +to_a+, then 
    # each of elements in the array will be turned into parameters/keys and 
    # concatenated into a single key. For example: 
    # 
    # expand_cache_key([:foo, :bar])    # => "foo/bar" 
    # expand_cache_key([:foo, :bar], "namespace") # => "namespace/foo/bar" 
    # 
    # The +key+ argument can also respond to +cache_key+ or +to_param+. 
    def expand_cache_key(key, namespace = nil) 
    expanded_cache_key = namespace ? "#{namespace}/" : "" 

    if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] 
     expanded_cache_key << "#{prefix}/" 
    end 

    expanded_cache_key << retrieve_cache_key(key) 
    expanded_cache_key 
    end 

는의 바로 가기를 정의 할 수 :

def cache(*args); ActiveSupport::Cache.expand_cache_key(*args); end 

그리고 가독성을 위해 :

ENV["RAILS_CACHE_ID"] = '' 

은 그래서 예를 들어, 재귀입니다 :

cache 'string' 
=> "/string" 

cache [1, 2] 
=> "/1/2" 

cache [1, 2, 3, 4] 
=> "/1/2/3/4" 

cache [1, 2], [3, 4] 
=> "[3, 4]//1/2" 

cache [[1, 2], [3, 4]] 
=> "/1/2/3/4" 

cache [@users, @products] 
=> "https://stackoverflow.com/users/207311-20140409135446308087000/users/207312-20140401185427926822000/products/1-20130531221550045078000/products/2-20131109180059828964000/products/1-20130531221550045078000/products/2-20131109180059828964000" 

cache @users.concat(@products) 
=> "https://stackoverflow.com/users/207311-20140409135446308087000/users/207312-20140401185427926822000/products/1-20130531221550045078000/products/2-20131109180059828964000/products/1-20130531221550045078000/products/2-20131109180059828964000" 

당신이 볼 수 있듯이, 두 번째 매개 변수가 너무 항상 배열에 매개 변수를 넣어, 네임 스페이스입니다.

제 질문에 답장하는 것은 같습니다.

관련 문제