<commit_before><?php

namespace Controllers\Api;

/**
 * Use azure table as cache.
 * Cache Rules:
 *  1. Cache clear daily on UTC time
 *  2. User must pass in cache ttl/expires in seconds
 */
class AzureCache extends \Controllers\Api\AzureTable
{

  public function beforeRoute()
  {
    parent::beforeRoute();
    $this->cache = \Cache::instance();
  }

  /**
   * get cache by name
   */
  public function getCache()
  {
    $this->params['tableName'] = 'cache' . date("Ym");
    $name                      = $this->params['name'];
    $rowKey                    = (9007199254740991 - time()) . '';
    $query                     = "(PartitionKey eq '$name') and (RowKey le '$rowKey')";
    $errors                    = array();
    $tableRst                  = $this->getTableName($errors);
    $tableName                 = $tableRst['tableName'];
    $result                    = null;

    if ($this->cache->exists("app-$$tableName-$name", $result)) {
      return $result;
    }

    $result = $this->execQuery($query, 1);
    if ($result['item']) {
      echo $result['item']['value'];
    }
  }

  /**
   * set cache
   */
  public function setCache()
  {

    $this->params['tableName'] = 'cache' . date("Ym");
    $name                      = $this->params['name'];
    $ttl                       = $this->getOrDefault('GET.ttl', 600);
    $expiresAt                 = time() + $ttl;
    $rowKey                    = (9007199254740991 - $expiresAt) . '';
    $errors                    = array();
    $tableRst                  = $this->getTableName($errors);
    $tableName                 = $tableRst['tableName'];

    $postBody = [
      'RowKey' => $rowKey,
      'value'  => $this->getOrDefault('GET.v', $this->f3->BODY),
      'ttl'    => $ttl,
      'expAt'  => date('c', $expiresAt),
    ];

    $data = [
      'items' => [$postBody],
    ];
    $result = $this->execTable($tableName, $name, $data);

    if ($result['item']) {
      $this->cache->set("app-$$tableName-$name", $result['item']['value'], $ttl);
      echo $result['item']['value'];
    }
  }<commit_msg>update cache<commit_after><?php

namespace Controllers\Api;

/**
 * Use azure table as cache.
 * Cache Rules:
 *  1. Cache clear daily on UTC time
 *  2. User must pass in cache ttl/expires in seconds
 */
class AzureCache extends \Controllers\Api\AzureTable
{

  public function beforeRoute()
  {
    parent::beforeRoute();
    $this->cache = \Cache::instance();
  }

  /**
   * get cache by name
   */
  public function getCache()
  {
    $this->params['tableName'] = 'cache' . date("Ym");
    $name                      = $this->params['name'];
    $rowKey                    = (9007199254740991 - time()) . '';
    $query                     = "(PartitionKey eq '$name') and (RowKey le '$rowKey')";
    $errors                    = array();
    $tableRst                  = $this->getTableName($errors);
    $tableName                 = $tableRst['tableName'];
    $result                    = null;

    if ($this->cache->exists("app-$$tableName-$name", $result)) {
      echo $result;
      return;
    }

    $result = $this->execQuery($query, 1);
    if ($result['item']) {
      echo $result['item']['value'];
      return;
    }
  }

  /**
   * set cache
   */
  public function setCache()
  {

    $this->params['tableName'] = 'cache' . date("Ym");
    $name                      = $this->params['name'];
    $ttl                       = $this->getOrDefault('GET.ttl', 600);
    $expiresAt                 = time() + $ttl;
    $rowKey                    = (9007199254740991 - $expiresAt) . '';
    $errors                    = array();
    $tableRst                  = $this->getTableName($errors);
    $tableName                 = $tableRst['tableName'];

    $postBody = [
      'RowKey' => $rowKey,
      'value'  => $this->getOrDefault('GET.v', $this->f3->BODY),
      'ttl'    => $ttl,
      'expAt'  => date('c', $expiresAt),
    ];

    $data = [
      'items' => [$postBody],
    ];
    $result = $this->execTable($tableName, $name, $data);
    echo $this->cache->set("app-$$tableName-$name", $postBody['value'], $ttl);
  }
{'fraction_non_alphanumeric': 0.18276446381021544, 'fraction_numerical': 0.01888162672476398, 'mean_word_length': 1.849655172413793, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '801459', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1292, 'n_words': 258}