2012-02-08 30 views
0

道歉,並提前感謝什麼,即使我輸入,似乎可能是愚蠢的問題,但無論如何這裏。Perl Catalyst DBIx遊標緩存 - 如何清除?

我使用DBIx基本催化劑申請::類與「作者」和相關的「書」表。此外,我還使用DBIx :: Class :: Cursor :: Cached來適當地緩存數據。

問題是,在編輯之後,我需要在實際過期之前清除緩存的數據。
1.)作者 - > show_author_and_books提取和緩存結果集。

2.)所述書本 - > edit_do這需要從作者 - > 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 :: Class :: Cursor :: Cached'設置'Caching'traits的MyApp :: Model :: DB定義。

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

traits  => [ 'Caching' ], 

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

- 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_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 
    ... 

} 

...  

- MyApp的::控制器:: Book.pm定義與更新簿條目,因此在show_author_and_books緩存的數據無效 'edit_do' 的方法。

... 
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_and_books_rs,如作者 - > show_author_and_books定義,包含方法 'clear_cache',但顯然這是超出範圍在書本 - > edit_do(更不用說可能會出現另一個問題)。

那麼,是再犯DBIx請求的正確方法,因爲每...... show_author_and_books,然後調用「clear_cache」再次證明還是有一個更直接的方式,我只能說像這樣$ C->高速緩存 - >( 'DB ::作者') - > clear_cache?

再次感謝您。

PS。我敢肯定,當我看到這個,明天問題的全面愚蠢會打我:■

回答

0

嘗試

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

謝謝你的建議,但唉,不能像D :: C :: Cursor中的所有數據一樣工作::緩存存儲在單個命名空間中(例如緩存將包含'DB :: Author'數據,'DB :: Books','DB :: Other'),所以清除將清除所有內容。我需要能夠使某個特定的集合或特定的命名空間過期,因此改爲使用以下解決方案。 – user647248 2012-02-13 22:15:15

0

我去解決方案到底是不是用「DBIx :: Class :: Cursor :: Cached',而是直接使用定義多個 後端高速緩存的Catalyst Cache插件來處理我試圖在現實世界場景中管理的不同名稱空間。

由於所有數據被保存在同一個命名空間中,所以我退出了D :: C :: Cursor :: Cached,並且看起來並沒有一種方法可以在 時間之前將數據過期。

因此,爲了完整性,從上面的代碼中,MyApp :: Model :: DB.pm定義將失去'traits'和'cursor_class'鍵/值。

則...

的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, ... 
    ... 
} 

... 

- MyApp的::控制器:: Book.pm定義與「edit_do」方法,更新簿條目,因此在show_author_and_books緩存數據無效。

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