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

Modify cache redis backend #11608

Closed
wants to merge 2 commits into from
Closed

Modify cache redis backend #11608

wants to merge 2 commits into from

Conversation

pantaovay
Copy link

Changes

  1. Modify cache redis backend, delete _PHCR
  2. Cache redis backend supports "client" option, now you can pass a redis client
  3. Redis's set method supports ttl,the call of settimeout is removed
  4. Redis has existsmethod to check if a key exists, the previous implementation uses get which is wrong

Why

  1. The redis cache backend use a special key _PHCR to store all keys used by cache. There is a issue: why the backend cache redis add the special key(statsKey) _PHCR #10905 .According to @Green-Cat ,it is disabled in 2.0.x branch which is not true. If use _PHCR, every time we set a cache, we have to run sAdd and set commands. Store all keys in _PHCR will cause it expands quickly and this will influence performance.
  2. The previous implementation mixes statsKey and prefix, it uses _PHCR as super prefix which is not needed.
  3. The cache backend doesn't support redis option which means that we can not pass a redis connection. So if we use modelsCache and modelsMetadata, there will be two redis connections. So I modify this part and let redis option be supported, and through the lifetime of a request we can use one redis connection.

# Changes

1. Modify cache redis backend, delete _PHCR
2. Cache redis backend supports "client" option, now you can pass a redis client

# Why

1. The redis cache backend use a special key _PHCR to store all keys used by cache. There is a issue: phalcon#10905 .According to @Green-Cat ,it is disabled in 2.0.x branch which is not true. If use _PHCR, every time we set a cache, we have to run sAdd and set commands. Store all keys in _PHCR will cause it expands quickly and this will influence performance.
2. The previous implementation mixes statsKey and prefix, it uses _PHCR as super prefix which is not needed.
3. The cache backend doesn't support redis option which means that we can not pass a redis connection. So if we use modelsCache and modelsMetadata, there will be two redis connections. So I modify this part and let redis option be supported, and through the lifetime of a request we can use one redis connection.
@pantaovay
Copy link
Author

See #11452

@pantaovay
Copy link
Author

@sergeyklay 😊

@sergeyklay sergeyklay added this to the 2.1.0 milestone Mar 30, 2016
@sergeyklay
Copy link
Contributor

I'll look into this as soon as I can

@@ -88,13 +99,21 @@ class Redis extends Backend implements BackendInterface
}

if !isset options["statsKey"] {
// Disable tracking of cached keys per default
let options["statsKey"] = "";
let options["statsKey"] = "_PHCR";
Copy link
Contributor

Choose a reason for hiding this comment

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

Modify cache redis backend, delete _PHCR

As I remember it was done in pantaovay@6b88422

Why are we again added _PHCR ?

Copy link
Author

Choose a reason for hiding this comment

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

Because it is inconsistent with the document. But current implementation is inconsistent with other cache backends😭. I will modify the document instead.

@JCMais
Copy link

JCMais commented May 30, 2016

Is that PR still being worked on?

@sergeyklay sergeyklay removed this from the 2.1.0 milestone Jun 16, 2016
@tigran-a
Copy link

tigran-a commented Jul 1, 2016

Hi, just to let you know, this _PHCR is a pain for us, it eats all the memory from our Redis: the keys are stored with TTL, so after they expire, redis removes it, but _PHCR keeps growing regardless if the keys exist or not (there is no explicit key removal from php, we rely on TTL). Why not to modify all cache backends that they'd interpret null value as not to use the stats key?

@tigran-a
Copy link

tigran-a commented Jul 1, 2016

Sorry for using a language that is not related to the project, but this what we have to run from time to time to clean up that key without affecting the production, maybe it might be useful for someone else:

import redis
import socket
KEY = '_PHCR'
r = redis.Redis(host=socket.gethostname())
c = r.scard(KEY)
print("%s has %d elements"%(KEY, c)) 
print("Starting removing the key values. This can take some time") 
t = r.sscan(KEY)
if t[1]:
    r.srem(KEY, *t[1])
while t[0]:
    t = r.sscan(KEY, t[0])
    if t[1]:
        r.srem(KEY, *t[1])
c = r.scard(KEY)
print("%s has %d elements"%(KEY, c)) 

@wcoppens
Copy link

Just to let you know, the _PHCR prefix really is annoying. We for instance use the redis cache across multiple applications. Phalcon in our case created the redis cache data since it acts as a receiver of measurement data. In Phalcon 1.x with the incubator package we did not have the prefix behavior and so all the cached data in Redis is not prefixed. However since we can't stay legacy we are forced to upgrade resulting in upgrading our whole application stack to support the prefix... It would really help us if there would come a solution in which the key is optional.

@sjinks sjinks closed this Apr 26, 2017
@sjinks sjinks reopened this Apr 26, 2017
@aat2703
Copy link

aat2703 commented Mar 7, 2018

Will this be added to 3.3.3? Really Looking forward to it 😁

@niden niden mentioned this pull request Nov 1, 2018
3 tasks
@niden
Copy link
Member

niden commented Nov 1, 2018

Closing in favor of #13563

@niden niden closed this Nov 1, 2018
niden added a commit that referenced this pull request Nov 4, 2018
* niden-feature/T11608-modify-redis-backend: (28 commits)
  [#11608] - Removed unused variables
  [#11608] - Reverting instantiation of the client
  [#11608] - Removed '_' from variables and methods
  [#11608] - Refactoring on the connect to check if the object is already initialized/connected
  [#11608] - Refactoring of the getClient()
  [#11608] - Fixed tabs
  [#11608] - Corrections based on review
  [#11608] - Corrected variable name
  [#11608] - Corrected costructor and changelog
  [#11608] - Updated the changelog
  [#11608] - Corrected tests
  [#11608] - Fixed typos
  [#11608] - More corrections
  [#11608] - Modifications to the class; Adjusted tests
  [#11608] - Changed the comparison for the key on save
  [#11608] - More corrections
  [#11608] - Changed the instatiation of redis
  [#11608] - Trying protected getClient
  [#11608] - Changed the getClient slightly
  [#11608] - Corrections to variables
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants