/
Cache.pm6
120 lines (85 loc) · 3.34 KB
/
Cache.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use v6;
use Web::Cache::Memory;
# TODO
# use Web::Cache::Disk;
# use Web::Cache::Memcached;
# use Web::Cache::Redis;
unit module Web::Cache:ver<0.000001>;
# Generate full module name from $backend shortname
sub mod-name(Str $backend --> Str) {
return 'Web::Cache::' ~ $backend.tc;
}
# Set a key / value in the cache
sub cache-set($store, $module, Str $key, Str $content --> Str) {
return &::($module ~ '::set')($store, $key, $content);
}
# Get a key from the cache
sub cache-get($store, Str $module, Str $key --> Str) {
return &::($module ~ '::get')($store, $key);
}
# Remove a key from the cache
sub cache-remove($store, Str $module, Str $key --> Str) {
return &::($module ~ '::remove')($store, $key);
}
# Clear the entire cache
sub cache-clear($store, Str $module --> Array) {
return &::($module ~ '::clear')($store);
}
# Build a new sub that provides interface to cache
# backend module and actions.
sub create-store-sub(:$backend_module, :%config --> Block) {
my $store_instance = &::($backend_module ~ '::load')(%config);
return -> Callable $content?, # Callback that generates the content for the cache
Mu:D :$store = $store_instance, # Actual cache instance
Str:D :$module = $backend_module, # Module that manages cache type
:$key, # Key for cache ID
:$expires_in, # Expire the provided key in n minutes
Bool :$clear = False, # Clears all keys from cache
Bool :$remove = False --> Str { # When passed with key, removes key from cache
# Remove a key or clear everything
when $clear { cache-clear( $store, $module ).Str }
when $remove { cache-remove( $store, $module, $key ) }
# Otherwise, store / return key from cache
cache-get( $store, $module, $key ) || cache-set( $store, $module, $key, $content.() );
}
}
# Cache store initialization
sub cache-create-store(Int :$size = 1024,
Str :$backend = 'memory' --> Block) is export {
my $module = mod-name($backend);
my %config = size => $size,
backend => $backend;
return create-store-sub(backend_module => $module, config => %config);
};
=begin pod
=head1 NAME
Web::Cache - Web framework independant caching module.
=head1 SYNOPSIS
=begin code
use Web::Cache;
my &memory-cache = cache-create-store( size => 2048,
backend => 'memory' );
memory-cache(key => $cache-key, {
my $data = expensive-db-query(...);
expensive-template-step($data);
});
memory-cache( key => $fancy_cache_key, :remove );
memory-cache( :clear );
=end code
=head1 DESCRIPTION
The goal of this module is to provide a variety of cache backend
wrappers and utilities that simplify caching tasks within the
context of a web application.
=head1 COPYRIGHT
This module is Copyright (c) 2017 Sam Morrison. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the Artistic License 2.0.
=head1 WARRANTY
This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
=head1 ISSUES
See this github repo issues tracker:
https://github.com/scmorrison/perl6-Web-Cache/issues
=head1 AUTHORS
Sam Morrison
=end pod
# vim: ft=perl6 sw=4 ts=4 st=4 sts=4 et