Skip to main content
added 60 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

A system state is for example the last time cron has run. So the State API \Drupal::state has nothing to do with caching at all.

Use insteadthe Cache API Drupal::cache() instead.

StoreExample how to store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set aYou can use the cache tag you've set above as fourth parameter so that you canto invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

A system state is for example the last time cron has run. So the State API has nothing to do with caching at all.

Use instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

A system state is for example the last time cron has run. So the State API \Drupal::state has nothing to do with caching at all.

Use the Cache API Drupal::cache() instead.

Example how to store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

You can use the cache tag you've set above as fourth parameter to invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);
added 117 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

A system state is for example the last time cron has run. So the State API has nothing to do with caching at all.

Use instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

Use instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

A system state is for example the last time cron has run. So the State API has nothing to do with caching at all.

Use instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);
added 4 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

IfWhen you want to store hard to compute data in memory you use a static variable is OK. ToBut to store thethis data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

It's not for caching, for this you should consider usingUse instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

If you want to store data in memory a static variable is OK. To store the data in the database the State API is not the right place:

The State API provides a place for developers to store information about the system's state.

It's not for caching, for this you should consider using Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);

When you want to store hard to compute data in memory you use a static variable. But to store this data in the database the State API is not the right place, despite the similar name:

The State API provides a place for developers to store information about the system's state.

Use instead Drupal::cache().

Store data in cache:

\Drupal::cache('data')->set($cid, $data, Cache::PERMANENT, ['heavy:' . $heavy_id]);

$cid is a unique cache key, which is used to retrieve the data from cache:

$data = \Drupal::cache('data')->get($cid);

Set a cache tag as fourth parameter so that you can invalidate the cache entry if it becomes outdated:

\Drupal\Core\Cache\Cache::invalidateTags(['heavy:1']);
added 89 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217
Loading
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217
Loading