forked from onPHP/onphp-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.class.php
More file actions
117 lines (98 loc) · 2.79 KB
/
Cache.class.php
File metadata and controls
117 lines (98 loc) · 2.79 KB
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
<?php
/****************************************************************************
* Copyright (C) 2005-2008 by Anton E. Lebedevich, Konstantin V. Arkhipov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
****************************************************************************/
/**
* System-wide access to selected CachePeer and DaoWorker.
*
* @see CachePeer
* @see http://onphp.org/examples.Cache.en.html
*
* @ingroup Cache
*
* @example cacheSettings.php
**/
final class Cache extends StaticFactory implements Instantiatable
{
const NOT_FOUND = 'nil';
const EXPIRES_FOREVER = 604800; // 7 days
const EXPIRES_MAXIMUM = 21600; // 6 hrs
const EXPIRES_MEDIUM = 3600; // 1 hr
const EXPIRES_MINIMUM = 300; // 5 mins
const DO_NOT_CACHE = -2005;
/// map dao -> worker
private static $map = null;
/// selected peer
private static $peer = null;
/// default worker
private static $worker = null;
/// spawned workers
private static $instances = array();
/**
* @return CachePeer
**/
public static function me()
{
if (!self::$peer || !self::$peer->isAlive())
self::$peer = new RuntimeMemory();
return self::$peer;
}
/* void */ public static function setPeer(CachePeer $peer)
{
self::$peer = $peer;
}
/**
* @return CachePeer
*/
public static function getPeer()
{
return self::$peer;
}
/* void */ public static function setDefaultWorker($worker)
{
Assert::classExists($worker);
self::$worker = $worker;
}
/**
* associative array, className -> workerName
**/
public static function setDaoMap($map)
{
self::$map = $map;
}
public static function appendDaoMap($map)
{
if (self::$map)
self::$map = array_merge(self::$map, $map);
else
self::setDaoMap($map);
}
/**
* @return BaseDaoWorker
**/
public static function worker($dao)
{
$class = get_class($dao);
if (!isset(self::$instances[$class])) {
if (isset(self::$map[$class])) {
$className = self::$map[$class];
self::$instances[$class] = new $className($dao);
} elseif ($worker = self::$worker)
self::$instances[$class] = new $worker($dao);
else
self::$instances[$class] = new CommonDaoWorker($dao);
}
return self::$instances[$class];
}
/* void */ public static function dropWorkers()
{
self::$instances = array();
}
}
?>