redis and bechmark classes
Showing
2 changed files
with
109 additions
and
0 deletions
lib/TZBenchMark.php
0 → 100644
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz\WordPress\Tools; | ||
| 4 | |||
| 5 | class TZBenchMark { | ||
| 6 | |||
| 7 | protected static $start_micro = 0; | ||
| 8 | protected static $end_micro = 0; | ||
| 9 | |||
| 10 | static function startTimer() { | ||
| 11 | self::$start_micro = microtime(true); | ||
| 12 | } | ||
| 13 | |||
| 14 | static function endTimer() { | ||
| 15 | self::$end_micro = microtime(true); | ||
| 16 | } | ||
| 17 | |||
| 18 | static function logTimer($prepend = "TZBENCHMARK TIME ====== ") { | ||
| 19 | error_log($prepend.(self::$end_micro - self::$start_micro)." seconds"); | ||
| 20 | } | ||
| 21 | |||
| 22 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
lib/TZRedis.php
0 → 100644
| 1 | <?php | ||
| 2 | |||
| 3 | namespace Tz\WordPress\Tools; | ||
| 4 | |||
| 5 | use Throwable; | ||
| 6 | use Predis; | ||
| 7 | |||
| 8 | class TZRedis { | ||
| 9 | |||
| 10 | protected static $redis = null; | ||
| 11 | private static $err = "TZRedis !!! "; | ||
| 12 | |||
| 13 | private static function connectInstance() { | ||
| 14 | |||
| 15 | try { | ||
| 16 | |||
| 17 | if(!self::$redis) { | ||
| 18 | self::$redis = new Predis\Client(); | ||
| 19 | } | ||
| 20 | |||
| 21 | } catch(Throwable $e) { | ||
| 22 | error_log(self::$err.$e->getMessage()); | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | static function set($key, $val, $expire = 60) { | ||
| 27 | try { | ||
| 28 | |||
| 29 | self::connectInstance(); | ||
| 30 | |||
| 31 | if(self::$redis) { | ||
| 32 | self::$redis->set($key, $val, 'ex', $expire); | ||
| 33 | } | ||
| 34 | |||
| 35 | }catch (Throwable $e) { | ||
| 36 | error_log(self::$err."Failed to set ".$key." with ".$val); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | static function get($key, $unserialize = false) { | ||
| 41 | try { | ||
| 42 | |||
| 43 | self::connectInstance(); | ||
| 44 | |||
| 45 | if(self::$redis) { | ||
| 46 | if($unserialize) { | ||
| 47 | unserialize(self::$redis->get($key)); | ||
| 48 | } else { | ||
| 49 | return self::$redis->get($key); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | }catch (Throwable $e) { | ||
| 54 | error_log(self::$err."Failed to get ".$key); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | static function exists($key) { | ||
| 59 | try { | ||
| 60 | |||
| 61 | self::connectInstance(); | ||
| 62 | |||
| 63 | if(self::$redis) { | ||
| 64 | return self::$redis->exists($key); | ||
| 65 | } | ||
| 66 | |||
| 67 | }catch (Throwable $e) { | ||
| 68 | error_log(self::$err.$e->getMessage()); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | static function clear($key) { | ||
| 73 | try { | ||
| 74 | |||
| 75 | self::connectInstance(); | ||
| 76 | |||
| 77 | if(self::$redis) { | ||
| 78 | return self::$redis->delete($key); | ||
| 79 | } | ||
| 80 | |||
| 81 | }catch (Throwable $e) { | ||
| 82 | error_log(self::$err.$e->getMessage()); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment