| 1 | package Treex::Core::CacheBlock; | ||||||||||
| 2 | 1 | $Treex::Core::CacheBlock::VERSION = '2.20210102'; | |||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Treex::Core::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Treex::Core::Resource; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Storable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Time::HiRes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | extends 'Treex::Core::Block'; | |||||||||
| 10 | |||||||||||
| 11 | #TODO: params could be obtained directly from block | ||||||||||
| 12 | |||||||||||
| 13 | 1 | has block => ( is => 'ro', isa => 'Treex::Core::Block' ); | |||||||||
| 14 | |||||||||||
| 15 | 1 | has cache => (is => 'ro', isa => 'Cache::Memcached'); | |||||||||
| 16 | 1 | has _loaded => (is => 'rw', isa => 'Bool', default => 0); | |||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 19 | 0 | my $self = shift; | |||||||||
| 20 | 0 | require Treex::Tool::Memcached::Memcached; | |||||||||
| 21 | |||||||||||
| 22 | 0 | return; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 0 | ▶ | override 'get_hash' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 26 | 0 | my $self = shift; | |||||||||
| 27 | 0 | return $self->block->get_hash(); | |||||||||
| 28 | 1 | }; | |||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub process_document { | ||||||||
|
Subroutine
process_document: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 31 | 0 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | 0 | my ($document) = pos_validated_list( | |||||||||
| 34 | \@_, | ||||||||||
| 35 | { isa => 'Treex::Core::Document' }, | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | 0 | $Storable::canonical = 1; | |||||||||
| 39 | |||||||||||
| 40 | # $document->set_hash(md5_hex($document->get_hash() . $self->block->get_hash())); | ||||||||||
| 41 | |||||||||||
| 42 | # my $document_hash = md5_hex(Storable::freeze($document)); | ||||||||||
| 43 | # log_info("CACHE: document_hash\t$document_hash"); | ||||||||||
| 44 | |||||||||||
| 45 | 0 | my $full_hash = $document->get_hash(); | |||||||||
| 46 | 0 | my $cached_document = $self->cache->get($full_hash); | |||||||||
| 47 | |||||||||||
| 48 | 0 | my $return_code = $Treex::Core::Block::DOCUMENT_PROCESSED; | |||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | if ( ! $cached_document ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | ▶ | if ( ! $self->_loaded() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | $self->block->process_start(); | |||||||||
| 53 | 0 | $self->block->_set_is_started(1); | |||||||||
| 54 | 0 | $self->_set_loaded(1); | |||||||||
| 55 | } | ||||||||||
| 56 | 0 | log_info("CACHE: calling process_document " . $self->block->get_block_name()); | |||||||||
| 57 | 0 | $self->block->process_document($document); | |||||||||
| 58 | #my $str = Storable::nfreeze($document); | ||||||||||
| 59 | #log_info("CACHE: Storing - $full_hash - $document - " . length($str) . "b"); | ||||||||||
| 60 | 0 | log_info("CACHE: Storing - $full_hash - $document"); | |||||||||
| 61 | 0 | $self->cache->set($full_hash, $document); | |||||||||
| 62 | # | ||||||||||
| 63 | # my $tmp_doc = $self->cache->get($full_hash); | ||||||||||
| 64 | # if ( ! $tmp_doc ) { | ||||||||||
| 65 | # sleep(1); | ||||||||||
| 66 | # log_info("CACHE: Storing - $full_hash - again!!"); | ||||||||||
| 67 | # log_info("SET: " . $self->cache->set($full_hash, $document)); | ||||||||||
| 68 | # log_info("ADD: " . $self->cache->add($full_hash, $document)); | ||||||||||
| 69 | # my $tmp_doc = $self->cache->get($full_hash); | ||||||||||
| 70 | # if ( ! defined($tmp_doc) ) { | ||||||||||
| 71 | # my $str = Storable::nfreeze($document); | ||||||||||
| 72 | # log_warn("Document size: " . length($str)); | ||||||||||
| 73 | # } | ||||||||||
| 74 | # } | ||||||||||
| 75 | # log_fatal("AAAAAAAAAAAAA") if ! $tmp_doc; | ||||||||||
| 76 | } else { | ||||||||||
| 77 | 0 | log_info("CACHE: loading from cache " . $self->block->get_block_name()); | |||||||||
| 78 | 0 | $_[0] = $cached_document; | |||||||||
| 79 | 0 | $return_code = $Treex::Core::Block::DOCUMENT_FROM_CACHE; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | $Storable::canonical = 0; | |||||||||
| 83 | |||||||||||
| 84 | 0 | return $return_code; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub get_required_share_files { | ||||||||
|
Subroutine
get_required_share_files: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 0 | my ($self) = @_; | |||||||||
| 89 | |||||||||||
| 90 | 0 | return $self->block->get_required_share_files(); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub process_end { | ||||||||
|
Subroutine
process_end: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 0 | my ($self) = @_; | |||||||||
| 95 | 0 | $self->block->process_end(); | |||||||||
| 96 | # default implementation is empty, but can be overriden | ||||||||||
| 97 | 0 | return; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | override 'get_block_name' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 101 | 0 | my $self = shift; | |||||||||
| 102 | 0 | ▶ | if ( ! defined($self->block) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | return ref($self); | |||||||||
| 104 | } else { | ||||||||||
| 105 | 0 | return $self->block->get_block_name(); | |||||||||
| 106 | } | ||||||||||
| 107 | 1 | }; | |||||||||
| 108 | |||||||||||
| 109 | 1 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||