wp_cache_get returning blank data

277 views Asked by At

I am using wp_cache_get function from init hook.

$this->loader->add_action('set_current_user', $fgb_2fa, 'restrict_user', 0, 0);
$this->loader->add_action('init', $fgb_2fa, 'restrict_user', 0, 0);

Function definition

private static function generate_pin($user_id, $from_api = false)
{
    $pin = substr(str_shuffle('1234567890'), 0, FGB_PIN_LENGTH);

    $data = array(
        'pin'      => $pin,
        'expires'  => time() + FGB_PIN_EXPIRES * 60,
    );

    //set data into cache
    FGB_Cache::set("DFGB2fa_data" . $user_id, $data, FGB_PIN_EXPIRES * 60);
}
    

public function restrict_user()
{
    static $done;

    global $current_user;
    self::$user = $current_user;
    $user_id = $current_user->ID;
    if (!$user_id) {
        self::$user = wp_get_current_user();
        $user_id = self::$user->ID;
    }

    if ($done || !$user_id) {
        return;
    }

    $done = true;

    self::restrict_and_verify();

    if ((!defined('DOING_AJAX') || !DOING_AJAX)
        && is_admin()
        && !is_super_admin()
    ) {
        if (cerber_get_user_policy('nodashboard', $user_id) && !wp_is_json_request()) {
            wp_redirect(home_url());
            exit;
        }
    }
}

public function restrict_and_verify()
{
    static $done = false;
    if ($done) {
        return;
    }
    $done = true;

    $user_id = get_current_user_id();

    $twofactor = self::get_2fa_data($user_id);

    if (empty( $twofactor['pin'] ) ) {
        return;
    }
    // exit;
}

    static function get_2fa_data($user_id = null)
{
    if (!$user_id) {
        $user_id = get_current_user_id();
    }

    $key = "DFGB2fa_data" . $user_id;
    $cache = FGB_Cache::get($key);
    error_log("GET cache");
    error_log($key);
    error_log($cache);
    return isset($cache['2fa']) ? $cache['2fa'] : array();
}

Here is my cache class. Whenever I set data using set function access cache from same function then it is working, but when I get data from get function of my cache class then it is returning blank data.

<?php

namespace FGB_API\Inc\Common;

final class FGB_Cache {
    private static $cache = array();
    private static $wp_cache_group = '';

    static function set( $key, $value, $expire = 0 ) {
        $exp = 0;

        if ( $expire > 0 ) {
            $exp = time() + (int) $expire;
            if ( $exp < time() ) {
                return false;
            }
        }

        $element = array( $value, $exp );
        self::$cache[ $key ] = $element;
        error_log("CACHE set from here");
        error_log("Value sent => ".json_encode($value));
        wp_cache_set( $key, json_encode($value), self::$wp_cache_group, $exp);

        //here it is working fine
        error_log("After set =>".wp_cache_get( $key, self::$wp_cache_group ));
        return true;
    }

    static function get( $key, $default = null ) {       
        //here it is not working
        $element = wp_cache_get( $key, self::$wp_cache_group, true );
        error_log("stored data");
        error_log($key);
        error_log($element);
        return $element;
    }

    static function delete( $key ) {
        error_log("Cache delete request");
        if ( isset( self::$cache[ $key ] ) ) {
            unset( self::$cache[ $key ] );
        }
        wp_cache_delete( $key, self::$wp_cache_group );
    }
}
0

There are 0 answers