Quantcast

Zend\Cache Return Value & Reference Success

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Zend\Cache Return Value & Reference Success

Mike Willbanks
Hello Folks,

Continuing from the discussion on IRC I just wanted to bring up a use
case that I may frequently hit due to cache sharing between a few
applications:

if (!$cache->getItem('theKey', array('namespace' => 'app'))) {

}

I know for myself I will be doing that far more than:

$value = $cache->getItem('theKey', array(), $success);
if (!$success) {
    .....
}

This in particular happens more often than not when you start getting
into more data sharing between applications which can become fairly
common with memcached.

Regards,

Mike
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Tomáš Fejfar
Sorry, can you please enlighten more what is the point - for someone who does not know what that "irc debate" was about? You can prefix/suffix the cache keys if sharing different cache entries is a problem, right? 

On Wed, Mar 28, 2012 at 8:37 PM, Mike Willbanks <[hidden email]> wrote:
Hello Folks,

Continuing from the discussion on IRC I just wanted to bring up a use
case that I may frequently hit due to cache sharing between a few
applications:

if (!$cache->getItem('theKey', array('namespace' => 'app'))) {

}

I know for myself I will be doing that far more than:

$value = $cache->getItem('theKey', array(), $success);
if (!$success) {
   .....
}

This in particular happens more often than not when you start getting
into more data sharing between applications which can become fairly
common with memcached.

Regards,

Mike

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Mike Willbanks
Tomáš,

> Sorry, can you please enlighten more what is the point - for someone who
> does not know what that "irc debate" was about? You can prefix/suffix the
> cache keys if sharing different cache entries is a problem, right?

Sorry about that; wanted to get the feedback out there prior to
another meeting and while I still had a bit of time.  In large, we
agreed in IRC that leveraging getItem() and having a default return
null if the item is not found but also allowing to have a reference
success variable was needed (in the event that the cached value is
allowed to be null - which in some special cases will be).  We got
into the conversation on the parameter order of if it would be:
getItem($key, $options = array(), &$success) OR getItem($key,
&$success, $options = array()) as the method signature.

My example was to show what you might be doing if you are accessing a
separate namespace in a different project to access that particular
key.  In the case of memcached there is libketama which allows for
consistent hashing, along with this you might set the hashing
algorithm to be consistent with that of other languages.  Which in
this event allows you to share the cache between multiple
applications.  So in this case: getItem($key, $options = array(),
&$success) is a better working model as you will likely be going
against different namespaces with a particular key vs. your
application default namespace.  I hope that it makes sense now!

>> Continuing from the discussion on IRC I just wanted to bring up a use
>> case that I may frequently hit due to cache sharing between a few
>> applications:
>>
>> if (!$cache->getItem('theKey', array('namespace' => 'app'))) {
>>
>> }
>>
>> I know for myself I will be doing that far more than:
>>
>> $value = $cache->getItem('theKey', array(), $success);
>> if (!$success) {
>>    .....
>> }

Regards,

Mike
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Marc Bennewitz (private)
In reply to this post by Mike Willbanks
Currently we discussed on zf2-meeting.
(@see
http://framework.zend.com/wiki/display/ZFDEV2/2012-03-28+Meeting+Agenda)

1. return NULL if the requested item wasn't found
2. allow to pass a success argument by reference to know if the returned
NULL value comes from a cache hit or missing item.

Now there are different opportunities to pass a success argument by
reference to getItem():

1. passing success argument by reference as second argument
getItem($key, & $success = null, array $options = array());

Pros:
 - Well known API also used by apc_fetch
Cons:
 - Not possible to set the options array without passing a success variable

2. passing success argument by reference as third argument
getItem($key, array $options = array(), & $success = null);

Pros:
 - Allows to set options array without success argument
Cons:
 - needs to set an options array to set the success reference


3. passing success argument by reference as option (no API change)
getItem($key, array $options = array());

Example:
$success = null;
$data = $cache->getItem($key, array('success' => & $success));
if ($success) // cache hit

Pros:
 - already used for the CAS tokens
Cons:
 - hard to explain how it works for developers new to PHP

I vote for passing success as option because it's already used for CAS
tokens and lets argument list simple.

Greetings
Marc

On 28.03.2012 20:37, Mike Willbanks wrote:

> Hello Folks,
>
> Continuing from the discussion on IRC I just wanted to bring up a use
> case that I may frequently hit due to cache sharing between a few
> applications:
>
> if (!$cache->getItem('theKey', array('namespace' => 'app'))) {
>
> }
>
> I know for myself I will be doing that far more than:
>
> $value = $cache->getItem('theKey', array(), $success);
> if (!$success) {
>     .....
> }
>
> This in particular happens more often than not when you start getting
> into more data sharing between applications which can become fairly
> common with memcached.
>
> Regards,
>
> Mike
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Enrico Zimuel-2

> 3. passing success argument by reference as option (no API change)
> getItem($key, array $options = array());
>
> Example:
> $success = null;
> $data = $cache->getItem($key, array('success' =>  &  $success));
> if ($success) // cache hit
>
> Pros:
>   - already used for the CAS tokens
> Cons:
>   - hard to explain how it works for developers new to PHP
>
>

I think this is a good solution because we take the advantage of a new
feature without change the API.
Because the 'success' option is for "advanced" scenarios I don't think
this will be a disadvantage for new PHP developers.
Personally, I never stored false or null values in a cache.

--
Enrico Zimuel
Senior PHP Engineer     | [hidden email]
Zend Framework Team     | http://framework.zend.com
Zend Technologies Ltd.
http://www.zend.com

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Marco Pivetta (Ocramius)
I'm also for the $cache->getItem($key, & $options)...
There is an issue with instructing newcomers, as metthew already pointed out, but I think that checking the return value of NULL or FALSE is an edge case that only some people will need to be aware of, and these people surely will have to look at the docs about return values of "getItem" :)
Marco Pivetta

http://twitter.com/Ocramius     

http://marco-pivetta.com    

On 29 March 2012 08:55, Enrico Zimuel <[hidden email]> wrote:

3. passing success argument by reference as option (no API change)
getItem($key, array $options = array());

Example:
$success = null;
$data = $cache->getItem($key, array('success' =>  &  $success));
if ($success) // cache hit

Pros:
 - already used for the CAS tokens
Cons:
 - hard to explain how it works for developers new to PHP



I think this is a good solution because we take the advantage of a new feature without change the API.
Because the 'success' option is for "advanced" scenarios I don't think this will be a disadvantage for new PHP developers.
Personally, I never stored false or null values in a cache.

--
Enrico Zimuel
Senior PHP Engineer     | [hidden email]
Zend Framework Team     | http://framework.zend.com
Zend Technologies Ltd.
http://www.zend.com


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Tomáš Fejfar
It's not ideal, but wouldn't this be more descriptive and discoverable:

$data = $cache->getItem($key);
if ($cache->isLastCallCacheHit()) // last call to getItem() was successful



On Thu, Mar 29, 2012 at 9:46 AM, Marco Pivetta <[hidden email]> wrote:
I'm also for the $cache->getItem($key, & $options)...
There is an issue with instructing newcomers, as metthew already pointed out, but I think that checking the return value of NULL or FALSE is an edge case that only some people will need to be aware of, and these people surely will have to look at the docs about return values of "getItem" :)
Marco Pivetta

http://twitter.com/Ocramius     

http://marco-pivetta.com    

On 29 March 2012 08:55, Enrico Zimuel <[hidden email]> wrote:

3. passing success argument by reference as option (no API change)
getItem($key, array $options = array());

Example:
$success = null;
$data = $cache->getItem($key, array('success' =>  &  $success));
if ($success) // cache hit

Pros:
 - already used for the CAS tokens
Cons:
 - hard to explain how it works for developers new to PHP



I think this is a good solution because we take the advantage of a new feature without change the API.
Because the 'success' option is for "advanced" scenarios I don't think this will be a disadvantage for new PHP developers.
Personally, I never stored false or null values in a cache.

--
Enrico Zimuel
Senior PHP Engineer     | [hidden email]
Zend Framework Team     | http://framework.zend.com
Zend Technologies Ltd.
http://www.zend.com



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Marc Bennewitz (private)
In reply to this post by Marco Pivetta (Ocramius)

> I'm also for the $cache->getItem($key, & $options)...
Not the hole options array will be passed by reference - only the "success" option ;)

> There is an issue with instructing newcomers, as metthew already pointed
> out, but I think that checking the return value of NULL or FALSE is an edge
> case that only some people will need to be aware of, and these people
> surely will have to look at the docs about return values of "getItem" :)
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://marco-pivetta.com
>
> On 29 March 2012 08:55, Enrico Zimuel <[hidden email]> wrote:
>
> >
> >  3. passing success argument by reference as option (no API change)
> >> getItem($key, array $options = array());
> >>
> >> Example:
> >> $success = null;
> >> $data = $cache->getItem($key, array('success' =>  &  $success));
> >> if ($success) // cache hit
> >>
> >> Pros:
> >>  - already used for the CAS tokens
> >> Cons:
> >>  - hard to explain how it works for developers new to PHP
> >>
> >>
> >>
> > I think this is a good solution because we take the advantage of a new
> > feature without change the API.
> > Because the 'success' option is for "advanced" scenarios I don't think
> > this will be a disadvantage for new PHP developers.
> > Personally, I never stored false or null values in a cache.
> >
> > --
> > Enrico Zimuel
> > Senior PHP Engineer     | [hidden email]
> > Zend Framework Team     | http://framework.zend.com
> > Zend Technologies Ltd.
> > http://www.zend.com
> >
> >
>

MfG

Marc Bennewitz
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Zend\Cache Return Value & Reference Success

Marc Bennewitz (private)
In reply to this post by Tomáš Fejfar
Tomas,

that's a very interesting approach (similar to ext/memcached) and I don't see issues with it.
Additionally it's possible to do more with it but it only makes sense to me if it would completely removed throwing exceptions by storage adapters and adding more result codes. (RS_SUCCESS, RS_EXPIRED, RS_NOT_FOUND, RS_NOT_STORED, RS_ERROR)
(all result codes greater than RS_ERROR are more specific errors differs by adapter)


Example:

$data   = $cache->getItem('key');
$rsCode = $cache->getLastResultCode();
if ($rsCode != Zend\Cache::RS_SUCCESS) {
    try {
            $data = $service->getData();
                $cache->save('key', $data);
        } catch (ServiceException $e) {
            // service failed -> check to use expired data from cache
                if ($rsCode == Zend\Cache::RS_EXPIRED) {
                    $data   = $cache->getItem('key', array('ttl' => 0));
                        $rsCode = $cache->getLastResultCode();
                }
               
                // if loading expired data from cache failed -> throw exception
                if ($rsCode != Zend\Cache::RS_SUCCESS) {
                    throw $e;
                }
        }
}

// $data can be used

Greetings
Marc

----- Original Nachricht ----
Von:     Tomá? Fejfar <[hidden email]>
An:      Marco Pivetta <[hidden email]>
Datum:   29.03.2012 11:11
Betreff: Re: [zf-contributors] Zend\Cache Return Value & Reference Success

> It's not ideal, but wouldn't this be more descriptive and discoverable:
>
> $data = $cache->getItem($key);
> if ($cache->isLastCallCacheHit()) // last call to getItem() was successful
>
>
> On Thu, Mar 29, 2012 at 9:46 AM, Marco Pivetta
> <[hidden email]>wrote:
>
> > I'm also for the $cache->getItem($key, & $options)...
> > There is an issue with instructing newcomers, as metthew already pointed
> > out, but I think that checking the return value of NULL or FALSE is an
> edge
> > case that only some people will need to be aware of, and these people
> > surely will have to look at the docs about return values of "getItem" :)
> > Marco Pivetta
> >
> > http://twitter.com/Ocramius
> >
> > http://marco-pivetta.com
> >
> > On 29 March 2012 08:55, Enrico Zimuel <[hidden email]> wrote:
> >
> >>
> >>  3. passing success argument by reference as option (no API change)
> >>> getItem($key, array $options = array());
> >>>
> >>> Example:
> >>> $success = null;
> >>> $data = $cache->getItem($key, array('success' =>  &  $success));
> >>> if ($success) // cache hit
> >>>
> >>> Pros:
> >>>  - already used for the CAS tokens
> >>> Cons:
> >>>  - hard to explain how it works for developers new to PHP
> >>>
> >>>
> >>>
> >> I think this is a good solution because we take the advantage of a new
> >> feature without change the API.
> >> Because the 'success' option is for "advanced" scenarios I don't think
> >> this will be a disadvantage for new PHP developers.
> >> Personally, I never stored false or null values in a cache.
> >>
> >> --
> >> Enrico Zimuel
> >> Senior PHP Engineer     | [hidden email]
> >> Zend Framework Team     | http://framework.zend.com
> >> Zend Technologies Ltd.
> >> http://www.zend.com
> >>
> >>
> >
>
Loading...