setOption(self::$conn::OPT_SERIALIZER, self::$conn::SERIALIZER_PHP); break; case 'SERIALIZER_IGBINARY': self::$conn->setOption(self::$conn::OPT_SERIALIZER, self::$conn::SERIALIZER_IGBINARY); break; default: self::$conn->setOption(self::$conn::OPT_SERIALIZER, self::$conn::SERIALIZER_NONE); break; } switch($compress){ case 'COMPRESSION_NONE': self::$conn->setOption(self::$conn::OPT_COMPRESSION, self::$conn::COMPRESSION_NONE); break; case 'COMPRESSION_ZSTD': self::$conn->setOption(self::$conn::OPT_COMPRESSION, self::$conn::COMPRESSION_ZSTD); self::$conn->setOption(self::$conn::OPT_COMPRESSION_LEVEL, (string) -5); break; case 'COMPRESSION_LZ4': self::$conn->setOption(self::$conn::OPT_COMPRESSION, self::$conn::COMPRESSION_LZ4); break; case 'COMPRESSION_LZF': self::$conn->setOption(self::$conn::OPT_COMPRESSION, self::$conn::COMPRESSION_LZF); break; } } catch(\RedisException $e){ error_log($e->getMessage()); throw new \Exception($e->getMessage()); // To show on settings page return false; } } static function get_conf(){ if(!file_exists(self::$conf_file)){ return []; } $conf = file_get_contents(self::$conf_file); if(empty($conf)){ error_log('SpeedyCache: Conf file was empty'); return; } return json_decode($conf, true); } // Creates a unique id based on blogID, group and key static function id($key, $group){ return self::$prefix . ':' . self::$blog_id.$group . ':' . $key; } // Updates object-cache.php file at wp-content/object-cache.php static function update_file(){ $file = WP_CONTENT_DIR . '/object-cache.php'; if(!file_exists($file)){ touch($file); } $code = '{$persistent}(self::$host); } else { self::$conn->{$persistent}(self::$host, self::$port); } if(!empty(self::$conf['username']) && !empty(self::$conf['password'])){ self::$conn->auth(['user' => self::$conf['username'], 'pass' => self::$conf['password']]); } else if(!empty(self::$conf['password'])){ self::$conn->auth(self::$conf['password']); } // Testing if connection worked $res = self::$conn->ping(); if(empty($res) && $res !== '+PONG'){ $failed = true; } if(!empty($failed)){ self::$conn = null; return false; } self::$conn->select((int) (!empty(self::$conf['db-id']) ? self::$conf['db-id'] : 0)); }catch(\RedisException $e){ error_log('SpeedyCache' . $e->getMessage()); throw new \Exception($e->getMessage()); // To show on settings page return false; } catch(\Exception $e){ error_log('SpeedyCache ->>' . $e->getMessage()); throw new \Exception($e->getMessage()); // To show on settings page } } return true; } static function set($key, $data, $expire = 0){ if(empty(self::$conn)){ return false; } $ttl = $expire ?: self::$ttl; if((is_array($data) || is_object($data)) && self::$serialize === 'none'){ $data = serialize($data); } $key = self::$prefix . $key; try{ $res = self::$conn->setEx($key, $ttl, $data); } catch (\RedisException $ex) { error_log($ex->getMessage()); } } static function get($key){ if(empty(self::$conn)){ return false; } $key = self::$prefix . $key; try{ return self::$conn->get($key); }catch(\RedisException $e){ error_log($e->getMessage()); return false; } } static function exists($key){ if(empty(self::$conn)){ return false; } $key = self::$prefix . $key; try{ return self::$conn->exists($key); }catch(\RedisException $e){ error_log($e->getMessage()); return false; } } static function delete($key){ $del = self::$async_flush ? 'unlink' : 'del'; if(empty(self::$conf['enable'])){ return false; } if(empty(self::$conn)){ return false; } $key = self::$prefix . $key; try{ self::$conn->{$del}($key); } catch(\RedisException $e){ error_log($e->getMessage()); return false; } } static function get_memory(){ self::boot(); if(empty(self::$conn)){ return 'None'; } try{ $memory = self::$conn->info('memory'); } catch(\RedisException $e){ error_log($e->getMessage()); return 'None'; } if(!empty($memory['used_memory'])){ return size_format($memory['used_memory']); } return 'None'; } // Flushes whole database static function flush_db($sync = true){ if(empty(self::$conf['enable'])){ return false; } if(empty(self::$conn)){ return false; } try{ return self::$conn->flushDb($sync); } catch(\RedisException $e){ error_log($e->getMessage()); return false; } } }