Skip to content

Commit 7ac7c1c

Browse files
committed
Minor fixes.
1 parent d0f77e5 commit 7ac7c1c

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

src/Cacheton.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace TonTon;
4+
5+
trait Cacheton {
6+
7+
/**
8+
* @var reference to multiton array of instances
9+
*/
10+
private static $instances = [];
11+
12+
/**
13+
* @var expiring time for caching
14+
*/
15+
protected $expires = 60;
16+
17+
/**
18+
* @var expiring time for caching
19+
*/
20+
protected $driver;
21+
22+
/**
23+
* Creates a new instance of a multiton class flagged with a key.
24+
*
25+
* @param $key the key which the instance should be stored/retrieved
26+
*
27+
* @return self
28+
*/
29+
final public static function instance($key)
30+
{
31+
32+
$cache = new \Memcached();
33+
$cache->addServer("localhost", 11211);
34+
35+
if(!array_key_exists($key, self::$instances)) {
36+
37+
$isCached = $cache->get($key);
38+
if(!$isCached) {
39+
self::$instances[$key] = new self;
40+
$cache->set($key, self::$instances[$key], 30);
41+
} else {
42+
self::$instances[$key] = new $isCached;
43+
echo 'Cached';
44+
}
45+
}
46+
47+
return self::$instances[$key];
48+
}
49+
50+
public function getExpire() {
51+
return $this->expires;
52+
}
53+
54+
public function setExpire(int $seconds) {
55+
$this->expires = $seconds;
56+
}
57+
58+
/**
59+
* Prevents calling the class using the new keyword, if the __construct is protected.
60+
*
61+
* @return void
62+
*/
63+
private function __construct() {}
64+
65+
/**
66+
* Prevents cloning the multiton instances.
67+
*
68+
* @return void
69+
*/
70+
final private function __clone() {}
71+
72+
/**
73+
* Prevents unserializing the multiton instances.
74+
*
75+
* @return void
76+
*/
77+
final private function __wakeup() {}
78+
}

src/Limiton.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,17 @@ public function setLimit(int $number) {
4545
self::$limit = $number;
4646
}
4747

48-
/**
49-
* Prevents calling the class using the new keyword, if the __construct is protected.
50-
*
51-
* @return void
52-
*/
53-
private function __construct() {}
54-
5548
/**
5649
* Prevents cloning the multiton instances.
5750
*
5851
* @return void
5952
*/
60-
final private function __clone() {}
53+
public function __clone() {}
6154

6255
/**
6356
* Prevents unserializing the multiton instances.
6457
*
6558
* @return void
6659
*/
67-
final private function __wakeup() {}
60+
private function __wakeup() {}
6861
}

src/Multiton.php

+2-9
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,17 @@ final public static function instance($key)
2626
return self::$instances[$key];
2727
}
2828

29-
/**
30-
* Prevents calling the class using the new keyword, if the __construct is protected.
31-
*
32-
* @return void
33-
*/
34-
private function __construct() {}
35-
3629
/**
3730
* Prevents cloning the multiton instances.
3831
*
3932
* @return void
4033
*/
41-
final private function __clone() {}
34+
private function __clone() {}
4235

4336
/**
4437
* Prevents unserializing the multiton instances.
4538
*
4639
* @return void
4740
*/
48-
final private function __wakeup() {}
41+
private function __wakeup() {}
4942
}

src/Singleton.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,27 @@ trait Singleton {
1717
*/
1818
final public static function instance()
1919
{
20+
2021
if(!self::$instance) {
2122

22-
self::$instance = new self;
23+
self::$instance = new self;
24+
2325
}
2426

2527
return self::$instance;
2628
}
2729

28-
/**
29-
* Prevents calling the class using the new keyword, if the __construct is protected.
30-
*
31-
* @return void
32-
*/
33-
private function __construct() {}
34-
3530
/**
3631
* Prevents cloning the singleton instance.
3732
*
3833
* @return void
3934
*/
40-
final private function __clone() {}
35+
private function __clone() {}
4136

4237
/**
4338
* Prevents unserializing the singleton instance.
4439
*
4540
* @return void
4641
*/
47-
final private function __wakeup() {}
42+
private function __wakeup() {}
4843
}

0 commit comments

Comments
 (0)