我创建了一个插件,它可以缓存它使用的一些数据,主要是Salesforce REST API调用结果,并每隔一段时间(默认每24小时)过期一次。
插件调用以下方法来控制缓存:
/**
* Check to see if this API call exists in the cache
* if it does, return the transient for that key
*
* @param string $url The API call we\'d like to make.
* @param array $args The arguents of the API call.
* @return get_transient $cachekey
*/
public function cache_get( $url, $args ) {
if ( is_array( $args ) ) {
$args[] = $url;
array_multisort( $args );
} else {
$args .= $url;
}
$prefix = esc_sql( $this->transient_prefix() );
$cachekey = $prefix . md5( wp_json_encode( $args ) );
return get_transient( $cachekey );
}
/**
* Create a cache entry for the current result, with the url and args as the key
*
* @param string $url The API query URL.
* @param array $args The arguments passed on the API query.
* @param array $data The data received.
* @param string $cache_expiration How long to keep the cache result around for.
* @return Bool whether or not the value was set
* @link https://developer.wordpress.org/reference/functions/set_transient/
*/
public function cache_set( $url, $args, $data, $cache_expiration = \'\' ) {
if ( is_array( $args ) ) {
$args[] = $url;
array_multisort( $args );
} else {
$args .= $url;
}
$prefix = esc_sql( $this->transient_prefix() );
$cachekey = $prefix . md5( wp_json_encode( $args ) );
// Cache_expiration is how long it should be stored in the cache.
// If we didn\'t give a custom one, use the default.
if ( \'\' === $cache_expiration ) {
$cache_expiration = $this->options[\'cache_expiration\'];
}
return set_transient( $cachekey, $data, $cache_expiration );
}
/**
* Get the cache transient prefix for this plugin and return it
*
* @return The transient prefix
*/
private function transient_prefix() {
$transient_prefix = \'sfwp\';
return $transient_prefix;
}
/**
* If there is a WordPress setting for how long to keep this specific cache, return it and set the object property
* Otherwise, return seconds in 24 hours
*
* @param string $option_key The cache item to keep around.
* @param int $expire The default time after which to expire the cache.
* @return The cache expiration saved in the database.
*/
public function cache_expiration( $option_key, $expire ) {
$cache_expiration = get_option( $option_key, $expire );
return $cache_expiration;
}
我想为用户提供一种手动清除此缓存的方法,以防他们更改Salesforce对象的配置,并且不想等待自动过期。我希望避免在这次事件中清除整个站点的缓存(这是我对wp_cache_flush()
可以)。我刚刚添加了$transient_prefix
在上面的方法中,我意识到如果站点只使用Transients API,这将很容易。但如果他们使用的是对象缓存,我就不行了。
我创建了一个cache_purge
方法如下:
/**
* Create a cache entry for the current result, with the url and args as the key
*
* @param string $subset If we only want to purge WordPress data, Salesforce data, options, etc.
* @return Bool whether or not the purge was successful
*/
public function cache_purge( $subset = \'\' ) {
$prefix = esc_sql( $this->transient_prefix() );
// cache is stored somewhere other than the options table
if ( wp_using_ext_object_cache() ) {
} else {
// cache is stored in the options table. this is pretty easy.
$options = $this->wpdb->options;
$t = esc_sql( \'_transient_timeout_\' . $prefix . \'%\' );
$sql = $wpdb ->prepare( "SELECT option_name FROM $options WHERE option_name LIKE \'%s\'", $t );
$transients = $this->wpdb->get_col( $sql );
foreach ( $transients as $transient ) {
// Strip away the WordPress prefix in order to arrive at the transient key.
$key = str_replace( \'_transient_timeout_\', \'\', $transient );
// Now that we have the key, use WordPress core to the delete the transient.
delete_transient( $key );
}
}
}
我的理解是,这将允许我检查是否存在任何外部对象缓存(我认为这将包括缓存插件,以及Varnish/memcache等),如果没有,请清除transients API。到目前为止我说的对吗?如果是这样,我该如何从对象缓存中清除相同的数据?