<?php

spl_autoload_register(function($class) {
  $file = __DIR__.'/predis/lib/'.strtr($class, '\\', '/').'.php';
  if (file_exists($file)) {
    require $file;
    return true;
  }
});

function ets_redis_get_client() {
  static $redis_client;
   
  if (!$redis_client) {
    $host = variable_get('ets_redis_host', 'localhost');
    $port = variable_get('ets_redis_port', 6379);
    $redis_client = new Predis\Client("tcp://{$host}:{$port}");
  }
  
  return $redis_client;
}

function ets_redis_get($key) {
  $client = ets_redis_get_client();
  return $client->get($key);
}

function ets_redis_set($key, $value) {
  $client = ets_redis_get_client();
  $client->set($key, $value);
}

function ets_redis_del($key) {
  $client = ets_redis_get_client();
  $client->del($key);
}

function ets_redis_experiment_key($nid) {
  // La logica di generazione di questa chiave è condivisa anche in experiment.js
  return 'experiment:' . $nid;
}

function ets_redis_user_key($uid) {
  // La logica di generazione di questa chiave è condivisa anche in user.js
  return 'user:' . $uid;
}
