道歉,並提前感謝什麼,即使我輸入,似乎可能是愚蠢的問題,但無論如何這裏。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。我敢肯定,當我看到這個,明天問題的全面愚蠢會打我:■
謝謝你的建議,但唉,不能像D :: C :: Cursor中的所有數據一樣工作::緩存存儲在單個命名空間中(例如緩存將包含'DB :: Author'數據,'DB :: Books','DB :: Other'),所以清除將清除所有內容。我需要能夠使某個特定的集合或特定的命名空間過期,因此改爲使用以下解決方案。 – user647248 2012-02-13 22:15:15