Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

include/classes/statscache.class.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
/**
5
 * A wrapper class used to store values transparently in memcache
6
 * Can be enabled or disabled through site configuration
7
 * Also sets a default time if no time is passed to it to enforce caching
8
 **/
9
class StatsCache {
10
  private $cache, $round;
11
12
  public function __construct($config, $debug) {
13
    $this->config = $config;
14
    $this->debug = $debug;
15
    if (! $config['memcache']['enabled'] ) {
16
      $this->debug->append("Not storing any values in memcache");
17
    } else {
18
      if (PHP_OS == 'WINNT') {
19
        require_once(CLASS_DIR . '/memcached.class.php');
20
      }
21
      $this->cache = new Memcached();
22
      if ($config['memcache']['sasl'] === true) {
23
        $this->cache->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
24
        $this->cache->setSaslAuthData($config['memcache']['sasl']['username'], $config['memcache']['sasl']['password']);
25
      }
26
    }
27
  }
28
29
  public function setRound($round_id) {
30
    $this->round = $round_id;
31
  }
32
  public function getRound() {
33
    return $this->round;
34
  }
35
36
  /**
37
   * Wrapper around memcache->set
38
   * Do not store values if memcache is disabled
39
   **/
40
  public function set($key, $value, $expiration=NULL) {
41
    if (! $this->config['memcache']['enabled']) return $value;
42 View Code Duplication
    if (empty($expiration))
43
      $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']);
44
    $this->debug->append("Storing " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
45
    return $this->cache->set($this->getRound() . '_' . $this->config['memcache']['keyprefix'] . $key, $value, $expiration);
46
  }
47
48
  /**
49
   * Special memcache->set call bypassing any auto-expiration systems
50
   * Can be used as a static, auto-updated cache via crons
51
   **/
52
  public function setStaticCache($key, $value, $expiration=NULL) {
53
    if (! $this->config['memcache']['enabled']) return $value;
54 View Code Duplication
    if (empty($expiration))
55
      $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']);
56
    $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3);
57
    if ($this->cache->set($this->config['memcache']['keyprefix'] . $key, $value, $expiration))
58
      return $value;
59
    return false;
60
  }
61
62
  /**
63
   * Wrapper around memcache->get
64
   * Always return false if memcache is disabled
65
   **/
66
  public function get($key, $cache_cb = NULL, &$cas_token = NULL) {
0 ignored issues
show
The parameter $cache_cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $cas_token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    if (! $this->config['memcache']['enabled']) return false;
68
    $this->debug->append("Trying to fetch key " . $this->getRound() . '_' . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
69 View Code Duplication
    if ($data = $this->cache->get($this->getRound() . '_' . $this->config['memcache']['keyprefix'].$key)) {
70
      $this->debug->append("Found key in cache", 3);
71
      return $data;
72
    } else {
73
      $this->debug->append("Key not found", 3);
74
    }
75
  }
76
77
  /**
78
   * As the static set call, we try to fetch static data here
79
   **/
80
  public function getStatic($key, $cache_cb = NULL, &$cas_token = NULL) {
0 ignored issues
show
The parameter $cache_cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $cas_token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    if (! $this->config['memcache']['enabled']) return false;
82
    $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3);
83 View Code Duplication
    if ($data = $this->cache->get($this->config['memcache']['keyprefix'].$key)) {
84
      $this->debug->append("Found key in cache", 3);
85
      return $data;
86
    } else {
87
      $this->debug->append("Key not found", 3);
88
    }
89
  }
90
91
  /**
92
   * Another wrapper, we want to store data in memcache and return the actual data
93
   * for further processing
94
   * @param key string Our memcache key
95
   * @param data mixed Our data to store in Memcache
96
   * @param expiration time Our expiration time, see Memcached documentation
97
   * @return data mixed Return our stored data unchanged
98
   **/
99
  public function setCache($key, $data, $expiration=NULL) {
100
    if ($this->config['memcache']['enabled']) $this->set($key, $data, $expiration);
101
    return $data;
102
  }
103
104
  /**
105
   * This method is invoked if the called method was not realised in this class
106
   **/
107
  public function __call($name, $arguments) {
108
    if (! $this->config['memcache']['enabled']) return false;
109
    //Invoke method $name of $this->cache class with array of $arguments
110
    return call_user_func_array(array($this->cache, $name), $arguments);
111
  }
112
}
113
114
$memcache = new StatsCache($config, $debug);
115
$memcache->addServer($config['memcache']['host'], $config['memcache']['port']);
116
// Now we can set our additional key prefix
117
if ($aTmpBlock = $block->getLast()) {
118
  $iRoundId = $aTmpBlock['id'];
119
} else {
120
  $iRoundId = 0;
121
}
122
$memcache->setRound($iRoundId);
123