Skip to content

Instantly share code, notes, and snippets.

@r-a-y
Last active July 28, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-a-y/3efa6fa0a4c51969ad023609cbc51f15 to your computer and use it in GitHub Desktop.
Save r-a-y/3efa6fa0a4c51969ad023609cbc51f15 to your computer and use it in GitHub Desktop.
BP Reply By Email - IMAP Object Cache extension. Replaces the default filesystem IMAP locking system in RBE with a persistent object cache.
<?php
/*
Plugin Name: BP Reply By Email - IMAP Object Cache extension
Description: Replaces the default filesystem IMAP locking system in RBE with a persistent object cache.
Author: r-a-y
Author URI: http://profiles.wordpress.org/r-a-y
License: GPLv2 or later
*/
/*
* Bail if we're not using a persistent object cache.
*
* This plugin has only been tested with Memcached and the WP PECL Memcached
* Object cache plugin:
* https://github.com/tollmanz/wordpress-pecl-memcached-object-cache
*
* It might work with other object cache plugins, but will need testing and
* possible modification.
*
* ---
*
* By default, RBE's IMAP checking uses the filesystem to place a file in the
* uploads folder to determine whether or not we are connected to the IMAP
* inbox. This technique works well on a single server set up, however if your
* site is on a load balancer, this filesystem check will not be as reliable.
*
* To counteract this and if your site is using an object cache like Memcached
* or Redis, deactivate RBE first, then activate this plugin and reactivate
* RBE.
*
* Be aware of the general nature of object caching. If your site generates a
* ton of cache items, it is possible that you will hit your cache limit. As
* such, our IMAP markers *could* get evicted, meaning we will have no
* reliable way of determining if the IMAP connection exists or how long the
* current connection is. If you regularly hit your cache memory limit,
* consider increasing it before enabling this option.
*/
if ( false === wp_using_ext_object_cache() ) {
return;
}
/**
* Registers RBE with WordPress' global cachegroups.
*/
function _bp_rbe_register_global_cachegroup() {
wp_cache_add_global_groups( 'bp_rbe' );
}
add_action( 'bp_loaded', '_bp_rbe_register_global_cachegroup' );
/** PLUGGABLES ***********************************************************/
function bp_rbe_is_connected( $args = array() ) {
$timestamp = wp_cache_get( 'connected', 'bp_rbe', true );
// Check if we're connected
if ( ! empty( $timestamp ) && time() <= ( $timestamp + bp_rbe_get_execution_time() + 15 ) ) {
return true;
}
return false;
}
function bp_rbe_is_connecting( $args = array() ) {
$timestamp = wp_cache_get( 'lock', 'bp_rbe', true );
// check if we're already attempting to connect
if ( ! empty( $timestamp ) && time() <= $timestamp + WP_CRON_LOCK_TIMEOUT ) {
return true;
}
return false;
}
function bp_rbe_stop_imap() {
wp_cache_set( 'stop', 1, 'bp_rbe' );
ray_log( 'rbe stop value: ' . wp_cache_get( 'stop', 'bp_rbe' ) );
remove_action( 'shutdown', 'bp_rbe_spawn_inbox_check' );
}
function bp_rbe_should_stop() {
$stop = wp_cache_get( 'stop', 'bp_rbe', true );
if ( false !== $stop ) {
//ray_log( 'should stop value: ' . var_export( $stop, true ) );
$test = wp_cache_delete( 'stop', 'bp_rbe' );
//ray_log( 'should stop cache delete: ' . var_export( $test, true ) );
return true;
}
return false;
}
function bp_rbe_add_imap_lock() {
wp_cache_set( 'lock', time(), 'bp_rbe' );
}
function bp_rbe_remove_imap_lock() {
wp_cache_delete( 'lock', 'bp_rbe' );
}
function bp_rbe_add_imap_connection_marker() {
wp_cache_set( 'connected', time(), 'bp_rbe' );
}
function bp_rbe_remove_imap_connection_marker() {
$connected = wp_cache_get( 'connected', 'bp_rbe', true );
//ray_log( 'connected value: ' . var_export( $connected, true ) );
if ( false !== $connected ) {
$test = wp_cache_delete( 'connected', 'bp_rbe' );
//ray_log( 'connected cache delete: ' . var_export( $test, true ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment