File Coverage

blib/lib/Dancer2/Session/Redis/Serialization/Sereal.pm
Criterion Covered Total %
statement 26 26 100.0
branch 3 6 50.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 2 2 100.0
total 42 47 89.3


line stmt bran cond sub pod time code
1             package Dancer2::Session::Redis::Serialization::Sereal;
2 1     1   4 use strictures 1;
  1         7  
  1         43  
3             # ABSTRACT: Dancer2::Session::Redis serialization broker for Sereal.
4             #
5             # This file is part of Dancer2-Session-Redis
6             #
7             # This software is Copyright (c) 2016 by BURNERSK .
8             #
9             # This is free software, licensed under:
10             #
11             # The MIT (X11) License
12             #
13              
14             BEGIN {
15 1     1   108 our $VERSION = '0.002'; # fixed version - NOT handled via DZP::OurPkgVersion.
16             }
17              
18 1     1   4 use Types::Standard qw( Bool InstanceOf );
  1         19  
  1         14  
19 1     1   507 use Moo;
  1         1  
  1         7  
20 1     1   231 use Sereal::Decoder qw( looks_like_sereal );
  1         2  
  1         58  
21 1     1   4 use Sereal::Encoder qw( SRL_UNCOMPRESSED SRL_SNAPPY );
  1         1  
  1         235  
22              
23             with 'Dancer2::Session::Redis::SerializationRole';
24              
25              
26             ############################################################################
27              
28             has snappy => (
29             is => 'ro',
30             isa => Bool,
31             default => 1,
32             );
33              
34             has _decoder => (
35             is => 'lazy',
36             isa => InstanceOf ['Sereal::Decoder'],
37 1     1   597 builder => sub { Sereal::Decoder->new },
38             );
39              
40             has _encoder => (
41             is => 'lazy',
42             isa => InstanceOf ['Sereal::Encoder'],
43             builder => sub {
44 1     1   457 my ($self) = @_;
45 1 50       4 my $snappy = $self->snappy ? SRL_SNAPPY : SRL_UNCOMPRESSED;
46 1         37 return Sereal::Encoder->new( {
47             compress => $snappy, # use Google Snappy compression algorithm if wanted.
48             compress_threshold => 1024, # compress when content is 1 kbyte or bigger.
49             croak_on_bless => 0, # do not croak on blessed objects.
50             undef_unknown => 0, # do not undef unknown objects.
51             stringify_unknown => 0, # do not stringify unknown objects.
52             warn_unknown => 1, # carp on unknown objects.
53             sort_keys => 0, # do not sort keys. we do not care abount sorting but performance.
54             } );
55             },
56             );
57              
58             ############################################################################
59              
60             sub decode {
61 1     1 1 3 my ( $self, $serialized_object ) = @_;
62             # deserealize stuff only if Sereal thinks it can do it.
63 1 50       5 return $serialized_object unless looks_like_sereal $serialized_object;
64 1         5 return $self->_decoder->decode($serialized_object);
65             }
66              
67              
68             sub encode {
69 2     2 1 2 my ( $self, $raw_object ) = @_;
70 2 50 33     11 return $raw_object if defined $raw_object && !ref $raw_object; # do not serialize simple scalars (strings).
71 2         15 return $self->_encoder->encode($raw_object);
72             }
73              
74              
75             ############################################################################
76              
77              
78             1;
79              
80             __END__