5 added 368 characters in body
source | link

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
    cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  }
  return $username;
}

Example by alandarev on Drupal.org, tweaked a bit by me.

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense - it only substitutes one query with another. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function, validators and callback function, then use static cache. Clive already described it&drupal_static($name, $default_value = NULL, $reset = FALSE) is what you need in such situations. Traditional way is to use it as his answer

$function_static_data = &drupal_static(__FUNCTION__);

This will make sure static data is properly associated with function that uses it, freeing you from the burden of collision prevention (as function names are unique in PHP already).

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
    cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  }
  return $username;
}

Example by alandarev on Drupal.org, tweaked a bit by me.

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
    cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  }
  return $username;
}

Example by alandarev on Drupal.org, tweaked a bit by me.

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense - it only substitutes one query with another. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function, validators and callback function, then use static cache. &drupal_static($name, $default_value = NULL, $reset = FALSE) is what you need in such situations. Traditional way is to use it as

$function_static_data = &drupal_static(__FUNCTION__);

This will make sure static data is properly associated with function that uses it, freeing you from the burden of collision prevention (as function names are unique in PHP already).

4 added 24 characters in body
source | link

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
  }
  cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  }
  return $username;
}

Example by alandarev on Drupal.org, tweaked a bit by me.

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
  }
  cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  return $username;
}

Example by alandarev on Drupal.org

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
    cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  }
  return $username;
}

Example by alandarev on Drupal.org, tweaked a bit by me.

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

3 added 81 characters in body
source | link

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
  }
  cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  return $username;
}

Example by alandarev on Drupal.org

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. AndThat's why Drupal can't cache to RAM on it's own. So Drupal caches in database. So unlessUnless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
  }
  cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  return $username;
}

Example by alandarev on Drupal.org

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. And Drupal caches in database. So unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference.


If you need the same data in few places, like form building function and callback function, then use static cache. Clive already described it in his answer.

You could just wrap your query in cache test, like this:

function steam_get_username2($steam64)  {
  if($cached = cache_get('steam'.$steam64, 'cache'))  {
    $username = $cached->data;
  }
  if(empty($username)) {
    $username = 'blank'; // Expensive code here
  }
  cache_set('steam'.$steam64, $username, 'cache', 60*60); //1 hour
  return $username;
}

Example by alandarev on Drupal.org

Please remember that HTTP is stateless protocol, so each time callback is called, it's a new execution, with new variables. That's why Drupal can't cache to RAM on it's own. So Drupal caches in database. Unless your query is really complicated, caching it makes little sense. You have to use APC or Memcached to really feel the difference, as these allows fast cache in RAM.


If you need the same data in few places during one call, like form building function and callback function, then use static cache. Clive already described it in his answer.

2 added 228 characters in body
source | link
    Post Undeleted by Mołot
    Post Deleted by Mołot
1
source | link