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

Add runtime cache to Zend_Locale_Data #918

Merged
merged 1 commit into from
Apr 21, 2020
Merged
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
32 changes: 26 additions & 6 deletions lib/Zend/Locale/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ class Zend_Locale_Data
*/
private static $_cacheDisabled = false;

/**
* Internal cache, prevent repeated cache requests
*
* @var array
*/
private static $_localCache = array();

/**
* Read the content from locale
*
Expand Down Expand Up @@ -335,8 +342,15 @@ public static function getList($locale, $path, $value = false)

$val = urlencode($val);
$id = self::_filterCacheId('Zend_LocaleL_' . $locale . '_' . $path . '_' . $val);

// add runtime cache to avoid callng cache backend multiple times during one request
if ( isset(self::$_localCache[$id])) {
return self::$_localCache[$id];
}
if (!self::$_cacheDisabled && ($result = self::$_cache->load($id))) {
return unserialize($result);
$result = unserialize($result);
self::$_localCache[$id] = $result;
return $result;
}

$temp = array();
Expand Down Expand Up @@ -946,11 +960,13 @@ public static function getList($locale, $path, $value = false)
}

if (isset(self::$_cache)) {
$data = serialize($temp);
if (self::$_cacheTags) {
self::$_cache->save( serialize($temp), $id, array('Zend_Locale'));
self::$_cache->save( $data, $id, array('Zend_Locale'));
} else {
self::$_cache->save( serialize($temp), $id);
self::$_cache->save( $data, $id);
}
static::$_localCache['id'] = $data;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmotyl
why this key used 'id' ?
should it be the $id variable ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn, it should. Can you make a PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and in line 1526 too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one reason, why its always good to wait for a second Review 👀

}

return $temp;
Expand Down Expand Up @@ -985,7 +1001,9 @@ public static function getContent($locale, $path, $value = false)
$val = urlencode($val);
$id = self::_filterCacheId('Zend_LocaleC_' . $locale . '_' . $path . '_' . $val);
if (!self::$_cacheDisabled && ($result = self::$_cache->load($id))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw if I understand correctly lack of

        // add runtime cache to avoid callng cache backend multiple times during one request
        if (isset(self::$_localCache[$id])) {
            return self::$_localCache[$id];
        }

before the if will trigger $_cache->load($id) and data is not taken from runtime

Noticed that after the patch we anyway have many Zend_LocaleC_ coming from cache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you propose a patch/pull request? thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1018
I've pushed the change, and tested it.

return unserialize($result);
$result = unserialize($result);
self::$_localCache[$id] = $result;
return $result;
}

switch(strtolower($path)) {
Expand Down Expand Up @@ -1499,11 +1517,13 @@ public static function getContent($locale, $path, $value = false)
$temp = current($temp);
}
if (isset(self::$_cache)) {
$data = serialize($temp);
if (self::$_cacheTags) {
self::$_cache->save( serialize($temp), $id, array('Zend_Locale'));
self::$_cache->save( $data, $id, array('Zend_Locale'));
} else {
self::$_cache->save( serialize($temp), $id);
self::$_cache->save( $data, $id);
}
static::$_localCache['id'] = $data;
}

return $temp;
Expand Down