Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve cache keys for acf-value-functions.php #403

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions includes/acf-value-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ function acf_get_value( $post_id = 0, $field ) {

// Get field name.
$field_name = $field['name'];

$field_key = $field['key'];

// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name" ) ) {
return $store->get( "$post_id:$field_name" );
if( $store->has( "$post_id:$field_name:$field_key" ) ) {
return $store->get( "$post_id:$field_name:$field_key" );
}

// Load value from database.
Expand All @@ -89,7 +90,7 @@ function acf_get_value( $post_id = 0, $field ) {
$value = apply_filters( "acf/load_value", $value, $post_id, $field );

// Update store.
$store->set( "$post_id:$field_name", $value );
$store->set( "$post_id:$field_name:$field_key", $value );

// Return value.
return $value;
Expand Down Expand Up @@ -121,11 +122,13 @@ function acf_format_value( $value, $post_id, $field ) {

// Get field name.
$field_name = $field['name'];

$field_key = $field['key'];

// Check store.
$store = acf_get_store( 'values' );
if( $store->has( "$post_id:$field_name:formatted" ) ) {
return $store->get( "$post_id:$field_name:formatted" );

if( $store->has( "$post_id:$field_name:$field_key:formatted" ) ) {
return $store->get( "$post_id:$field_name:$field_key:formatted" );
}

/**
Expand All @@ -141,7 +144,7 @@ function acf_format_value( $value, $post_id, $field ) {
$value = apply_filters( "acf/format_value", $value, $post_id, $field );

// Update store.
$store->set( "$post_id:$field_name:formatted", $value );
$store->set( "$post_id:$field_name:$field_key:formatted", $value );

// Return value.
return $value;
Expand Down Expand Up @@ -196,7 +199,7 @@ function acf_update_value( $value = null, $post_id = 0, $field ) {
acf_update_metadata( $post_id, $field['name'], $field['key'], true );

// Delete stored data.
acf_flush_value_cache( $post_id, $field['name'] );
acf_flush_value_cache( $post_id, $field['name'], $field['key'] );

// Return update status.
return $return;
Expand Down Expand Up @@ -244,12 +247,15 @@ function acf_update_values( $values = array(), $post_id = 0 ) {
* @param string $field_name The field name.
* @return void
*/
function acf_flush_value_cache( $post_id = 0, $field_name = '' ) {
function acf_flush_value_cache( $post_id = 0, $field_name = '', $field_key = '' ) {

// Delete stored data.
acf_get_store( 'values' )
->remove( "$post_id:$field_name" )
->remove( "$post_id:$field_name:formatted" );
->remove( "$post_id:$field_name:formatted" )
->remove( "$post_id:$field_name:$field_key" )
->remove( "$post_id:$field_name:$field_key:formatted" )
;
}

/**
Expand Down Expand Up @@ -285,7 +291,7 @@ function acf_delete_value( $post_id, $field ) {
acf_delete_metadata( $post_id, $field['name'], true );

// Delete stored data.
acf_flush_value_cache( $post_id, $field['name'] );
acf_flush_value_cache( $post_id, $field['name'], $field['key'] );

// Return delete status.
return $return;
Expand Down