2012-02-08 4 views
0

은 내가 입력 할 때조차도 어리석은 질문처럼 보이지만 사전에 감사드립니다. 그러나 어쨌든 여기에 있습니다.Perl 촉매 DBIx 커서 캐시 - 삭제 방법?

나는 DBIx를 사용하여 기본 촉매 응용 프로그램 : 클래스에 '저자'와 관련된 '책'테이블이있다. 또한 DBIx :: Class :: Cursor :: Cached을 적절하게 사용하여 데이터를 캐시합니다.

문제는 편집 후에 실제로 만료되기 전에 캐시 된 데이터를 삭제해야한다는 것입니다.
) 저자 -> show_author_and_books 결과 집합을 가져오고 캐시합니다.

2)를 Book-> edit_do Author-> show_author_and_books 요청에서 캐시 된 데이터를 삭제해야.

아래의 기본/적절한 설정을 참조하십시오.

- MyApp.pm 정의에는 백엔드 'Cache :: FileCache'캐시가 포함됩니다.

__PACKAGE__->config(
name   => 'MyApp', 
... 

'Plugin::Cache' => { 'backend' => { class    => 'Cache::FileCache', 
             cache_root   => "./cache", 
             namespace   => "dbix", 
             default_expires_in => '8 hours', 
             auto_remove_stale => 1 
            } 
        }, 
... 

- '캐시'특성은 'DBIx이 :: 클래스 :: 커서 :: 캐시'를 사용하여 세트의 MyApp :: 모델 :: DB 정의.

... 
__PACKAGE__->config(
schema_class => 'MyApp::Schema', 

traits  => [ 'Caching' ], 

connect_info => { dsn   => '<dsn>', 
        user   => '<user>', 
        password  => '<password>', 
        cursor_class => 'DBIx::Class::Cursor::Cached' 
       } 
); 
... 

- 결과 집합 캐시 - 'show_author_and_books'방법에의 MyApp :: 컨트롤러 :: Author.pm 정의.

... 
sub show_author_and_books :Chained('base') :PathPart('') :Args(0) 
{ 
    my ($self, $c) = @_; 

    my $author_id = $c->request->params->{author_id}; 

    my $author_and_books_rs = $c->stash->{'DB::Author'}->search({ author_id => $author_id }, 
                  { prefetch => 'book' }, 
                   cache_for => 600 }); # Cache results for 10 minutes. 

    # More interesting stuff, but no point calling $author_and_books_rs->clear_cache here, it would make no sense:s 
    ... 

} 

...  

- 번호부 항목을 업데이트하고 그래서 show_author_and_books에 캐시 된 데이터를 무효화 'edit_do'방법의 MyApp :: 컨트롤러 :: Book.pm 정의.

... 
sub edit_do :Chained('base') :PathPart('') :Args(0) 
{ 
    my ($self, $c) = @_; 

    # Assume stash contains a book for some author, and that we want to update the description. 

    my $book = $c->stash->{'book'}->update({ desc => $c->request->params->{desc} }); 

    # How do I now clear the cached DB::Author data to ensure the new desc is displayed on next request to 'Author->show_author_and_books'? 

    # HOW DO I CLEAR CACHED DB::Author DATA? 

    ... 
} 

은 당연히 나는 Author-> show_author_and_books에 정의 된 $ author_and_books_rs가, 방법 'clear_cache'를 포함 알고 있어요,하지만 분명히이 Book-> edit_do의 범위를 벗어 (다른 문제는있을 수 있음).

그래서 다시 DBIx 요청을 할 수있는 올바른 방법은 ... 에 따라, show_author_and_books 한 후 전화를 다시 'clear_cache'그게 아니면 그냥이 $ 같은 것을 말할 수있는보다 직접적인 방법이있다 c-> cache -> ('DB :: Author') -> clear_cache?

다시 한 번 감사드립니다.

추신.나는이 내일 볼 때 확실 해요, 질문의 전체 어리 석음이 나를 때리는 것 :의

답변

0

시도

$c->model('DB::Author')->clear_cache() ; 
+0

제안 해 주셔서 감사합니다.하지만 아아는 D :: C :: Cursor의 모든 데이터로 작동하지 않습니다. :: 단일 네임 스페이스 (예 : 캐시에 'DB :: Author'데이터, 'DB :: Books', 'DB :: Other'가 포함되어 있음)에 캐시되어 있으므로 모든 것이 명확 해집니다. 특정 세트 나 특정 네임 스페이스를 만료시킬 수 있어야하므로 대신 다음 해결책을 찾아야합니다. – user647248

0

내가 들어갑니다 솔루션을 결국 'DBIx를 사용하지하는 것이었다 :: Class :: Cursor :: Cached '대신 실제 캐시 시나리오에서 관리하려는 다른 네임 스페이스를 처리하기 위해 복수 백엔드 캐시를 정의하는 Catalyst 캐시 플러그인을 직접 사용합니다.

D :: C :: Cursor :: 모든 데이터가 동일한 네임 스페이스에 저장되어 있기 때문에 캐싱되었습니다. 더하기 데이터가 이미 만료 된 메서드 인 것처럼 보입니다. 시간이 이미 설정되었습니다.

그래서 위 코드에서 MyApp :: Model :: DB.pm 정의는 '특성'및 '커서 _ 클래스'키/값을 잃게됩니다. 그런 다음

...

MyApp.pm 플러그인 : 캐시 '여러 캐시 네임 스페이스를 포함하도록 확장 할

...

-- MyApp.pm definition including backend 'Cache::FileCache' cache. 

... 
'Plugin::Cache' => { 'backends' => { Authors => { class    => 'Cache::FileCache', 
                cache_root   => "./cache", 
                namespace   => "Authors", 
                default_expires_in => '8 hours', 
                auto_remove_stale => 1 
               }, 
            CDs => { class      => 'Cache::FileCache', 
                 cache_root   => "./cache", 
                 namespace   => "CDs", 
                 default_expires_in => '8 hours', 
                 auto_remove_stale => 1 
               }, 
            ...    
            } 
...         

-의 MyApp :: 컨트롤러 :: Author.pm 정의 'show_author_and_books'메소드가있는 경우 - 결과 집합이 캐시됩니다.

... 
sub show_author_and_books :Chained('base') :PathPart('') :Args(0) 
{ 
    my ($self, $c) = @_; 

    my $author_id = $c->request->params->{author_id}; 

    my $author = $c->get_cache_backend('Authors')->get($author_id); 

    if(!defined($author)) 
    { 
     $author = $c->stash->{'DB::Author'}->search({ author_id => $author_id }, 
                { prefetch => 'book', rows => 1 })->single; 

     $c->get_cache_backend('Authors')->set($author_id, $author, "10 minutes"); 
    }                

    # More interesting stuff, ... 
    ... 
} 

... 

- 번호부 항목을 업데이트하고 그래서 show_author_and_books에서 캐시 된 데이터를 무효화 'edit_do'방법의 MyApp :: 컨트롤러 :: Book.pm 정의.

... 
sub edit_do :Chained('base') :PathPart('') :Args(0) 
{ 
    my ($self, $c) = @_; 

    # Assume stash contains a book for some author, and that we want to update the description. 

    my $book = $c->stash->{'book'}->update({ desc => $c->request->params->{desc} }); 

    # How do I now clear the cached DB::Author data to ensure the new desc is displayed on next request to 'Author->show_author_and_books'? 

    # HOW DO I CLEAR CACHED DB::Author DATA? THIS IS HOW, EITHER... 

    $c->get_cache_backend('Authors')->set($c->stash->{'book'}->author_id, {}, "now"); # Expire now. 

    # ... OR ... THE WHOLE Authors namespace... 

    $c->get_cache_backend('Authors')->clear;  

    ... 
} 

참고 : 저자와 CD의 사용에서 기대하는 것,이 내가 일하고 있어요 실제 시나리오 아니라, 내 의도를 보여주기 위해 봉사해야한다.

나는 DBIx와 실제로 Catalyst의 경이로움에 비교적 익숙하기 때문에 더 나은 접근법이 있다면 듣고 싶다. (나는 거기에있을 것을 매우 기대한다.) 그러나 잠시 동안 기존 응용 프로그램을 업데이트하려고합니다.

0

결과 집합 캐시를 네임 스페이스에 쉽게 적용하고 독립적으로 지우도록 플러그인을 패치 할 수 있으며, 특성에 네임 스페이스를 추가하는 것이 그리 힘들지 않을 수도 있습니다. # dbix-class 히트 작업을하고 싶다면 멘토르에게 기꺼이 맡기십시오 - jnap