line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
394
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
2
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
3
|
|
|
|
|
|
|
package Cache::Reddit; |
4
|
|
|
|
|
|
|
$Cache::Reddit::VERSION = '0.02'; |
5
|
|
|
|
|
|
|
#ABSTRACT: a caching API that uses Reddit as the backend |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
9
|
|
|
|
|
|
|
our @EXPORT = qw/get set remove/; |
10
|
1
|
|
|
1
|
|
400
|
use Reddit::Client; |
|
1
|
|
|
|
|
64424
|
|
|
1
|
|
|
|
|
64
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $session_file = '~/.reddit'; |
14
|
|
|
|
|
|
|
my $reddit = Reddit::Client->new( |
15
|
|
|
|
|
|
|
session_file => $session_file, |
16
|
|
|
|
|
|
|
user_agent => 'Cache::Reddit/0.1', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
6
|
use Storable (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
231
|
|
20
|
|
|
|
|
|
|
|
21
|
0
|
|
|
0
|
0
|
|
sub serialize { Storable::nfreeze($_[0]) } |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
0
|
0
|
|
sub deserialize { Storable::thaw($_[0]) } |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub authenticate |
26
|
|
|
|
|
|
|
{ |
27
|
0
|
0
|
0
|
0
|
0
|
|
die 'Cache::Reddit requires the following environment variables: reddit_username, reddit_password and reddit_subreddit' |
|
|
|
0
|
|
|
|
|
28
|
|
|
|
|
|
|
unless $ENV{reddit_username} && $ENV{reddit_password} && $ENV{reddit_subreddit}; |
29
|
0
|
0
|
|
|
|
|
unless ($reddit->is_logged_in) { |
30
|
0
|
|
|
|
|
|
$reddit->login($ENV{reddit_username}, $ENV{reddit_password}); |
31
|
0
|
|
|
|
|
|
$reddit->save_session(); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub set |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
|
|
0
|
1
|
|
my ($value) = @_; |
39
|
|
|
|
|
|
|
|
40
|
0
|
0
|
0
|
|
|
|
die 'set() only accepts references' unless $value && ref $value; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
authenticate(); |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $data = serialize($value); |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
$reddit->submit_text( |
47
|
|
|
|
|
|
|
subreddit => $ENV{reddit_subreddit}, |
48
|
|
|
|
|
|
|
title => 'Cache::Reddit::' . (int rand 100000), |
49
|
|
|
|
|
|
|
text => $data, |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub get |
55
|
|
|
|
|
|
|
{ |
56
|
0
|
|
|
0
|
1
|
|
my $key = shift; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
die 'get() requires a key argument' unless $key; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
authenticate(); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
|
my $data; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
for my $link ( @{$reddit->fetch_links(subreddit => 'cache_reddit')->{items}} ) |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
{ |
66
|
0
|
0
|
|
|
|
|
if ($link->{name} eq $key) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
|
$data = $link->{selftext}; |
69
|
0
|
|
|
|
|
|
last; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
0
|
0
|
|
|
|
|
deserialize($data) if $data; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub remove |
77
|
|
|
|
|
|
|
{ |
78
|
0
|
|
|
0
|
1
|
|
my $key = shift; |
79
|
0
|
0
|
|
|
|
|
die 'remove() requires a key argument' unless $key; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
authenticate(); |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
$reddit->delete_item(name => $key); |
84
|
0
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
__END__ |