LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / LocalCache.pm
blob2efd89c4a5d574be605a044fac07ddcf827e1147
1 package LJ::LocalCache;
3 use strict;
4 use warnings;
6 use LJ::ModuleLoader;
8 # Usage example:
9 #
10 # LJ::LocalCache::get_cache()->set("ml.${lncode}.${dmid}.${itcode}", $text, 30*60);
14 my @SUBCLASSES = LJ::ModuleLoader->module_subclasses(__PACKAGE__);
15 my %modules_info;
16 my $loaded = 0;
18 use constant {
19 LOAD_SUCCESSFUL => 1,
20 LOAD_FAILED => -1,
21 NOT_LOADED => 0,
22 CRITICAL_ERROR => 2,
25 sub __load_packages {
26 return if $loaded;
28 if (LJ::is_enabled("local_cache")) {
29 %modules_info = map { $_ => NOT_LOADED } @SUBCLASSES;
30 $loaded = 1;
34 # this function will disable a module from using
35 # even it is already loaded
36 sub __critical_error {
37 my $module = shift;
38 $modules_info{$module} = CRITICAL_ERROR;
41 sub __get_package {
42 my $module = shift;
43 my $status = $modules_info{$module};
45 return "LJ::LocalCache::$module" if $status == LOAD_SUCCESSFUL;
46 return 'LJ::LocalCache' if $status == LOAD_FAILED ||
47 $status == CRITICAL_ERROR;
49 # try to load
50 eval "use $module" ;
52 if ($@) {
53 $modules_info{$module} = LOAD_FAILED;
54 return 'LJ::LocalCache';
57 return $module;
60 sub get_cache {
61 my ($handler) = @_;
62 $handler ||= $LJ::LOCAL_CACHE_DEFAULT_HANDLER;
64 return 'LJ::LocalCache' if !LJ::is_enabled("local_cache");
65 __load_packages();
66 return __get_package("LJ::LocalCache::$handler");
69 sub expire {
70 my ($class, $key, $expire) = @_;
71 return 0;
74 sub get {
75 my ($class, $key) = @_;
76 return undef;
79 sub get_multi {
80 my ($class, $keys, $not_fetched_keys) = @_;
81 @$not_fetched_keys = @$keys;
82 return undef;
85 sub set {
86 my ($class, $key, $value, $expire) = @_;
87 return undef;
90 sub replace {
91 my ($class, $key, $value, $expire) = @_;
92 return 0;
95 sub delete {
96 my ($class, $key) = @_;
97 return 0;
100 sub incr {
101 my ($class, $key, $value) = @_;
102 return 0;
105 sub decr {
106 my ($class, $key, $value) = @_;
107 return 0;
110 sub exists {
111 my ($class, $key) = @_;
112 return 0;
115 sub rpush {
116 my ($class, $key, $value) = @_;
117 return 0;
120 sub lpush {
121 my ($class, $key, $value) = @_;
122 return 0;
125 sub lpop {
126 my ($class, $key) = @_;
127 return undef;
130 sub rpop {
131 my ($class, $key) = @_;
132 return undef;