Per-item cache TTL

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Per-item cache TTL

Julian Vidal
As mentioned in this pull
request<https://github.com/zendframework/zf2/pull/5386>ZF2 does not
provide per-item cache TTL like ZF1 did.

I'm using per-item TTL all throughout a ZF1 app which I'm currently
migrating to ZF2 and desperately need to be able to do this. If I need to
configure another cache adapter for every different TTL, my config file be
huge and very hard to maintain.

At some point in the discussion, Matthew Weier O'Phinney suggests:
"Implement a plugin to add Item based TTL for storages not supporting it
natively"

Unfortunately, as I mention on my
comment<https://github.com/zendframework/zf2/pull/5386#issuecomment-34491161>,
I don't know how to write this kind of plugin. My deadline is approaching
so I will appreciate ANY pointers on this matter.

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Per-item cache TTL

Marc Bennewitz
Julian,


Am 11.02.2014 18:41, schrieb Julian Vidal:
> As mentioned in this pull
> request<https://github.com/zendframework/zf2/pull/5386>ZF2 does not
> provide per-item cache TTL like ZF1 did.
That's not correct 100%.
ZF2 cache storage adapter doesn't change TTL behavior of the storage itself.
So if the storage itself (like memcached, apc, xcache, ...) supports it
the ZF2 cache storage adapter supports it, too.

> I'm using per-item TTL all throughout a ZF1 app which I'm currently
> migrating to ZF2 and desperately need to be able to do this. If I need to
> configure another cache adapter for every different TTL, my config file be
> huge and very hard to maintain.
The TTL is an option of a storage adapter as it's an option how the
storage should handle items in it.
If you have different TTL definitions normally you have different types
of items which means you would need different configurations and storage
instances.
If you don't like that you need to write your own simple wrapper against
a storage as @Ocramius prototyped here:
https://github.com/zendframework/zf2/pull/5386#issuecomment-29381538

> At some point in the discussion, Matthew Weier O'Phinney suggests:
> "Implement a plugin to add Item based TTL for storages not supporting it
> natively"
>
> Unfortunately, as I mention on my
> comment<https://github.com/zendframework/zf2/pull/5386#issuecomment-34491161>,
> I don't know how to write this kind of plugin. My deadline is approaching
> so I will appreciate ANY pointers on this matter.
As @weierophinney pointed out "... for storages not supporting it
natively" - it's a great way of implementing it to make that behavior
available for storages like the filesystem or dba. It doesn't magically
change the API and allows a TTL argument ... -> it needs to fetch the
configured TTL option, calculate the expected expired time in base of
current time + TTL and store it together with the item. On reading it
needs to check the stored expire time and return a "Item not found"
behavior in cases it's expired.
An implementation would look similar to the current serializer plugin.

Currently I don't have enough time to do some more work I'm interested
in as we born our (first) adorable daughter :)

But I could assist :)

> Thanks!

Marc


--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Per-item cache TTL

Julian Vidal
Marc,

First of all, congratulations on your new baby. Say goodbye to your long
night sleep for me :)

I'm not going to pretend I fully understand the discussion in the github PR
as obviously there are very strong opinions on both sides. All I need is to
make it work; and fast since I don't have the luxury of time for this
particular issue. It seems to me that the discussion revolves around the
responsibilities of the adapter; what it should (or not) do. I can't really
support either side as I don't have the technical knowledge for that. All I
can say is that the ZF1 way of doing this thing seemed much more flexible
but if I can find a workaround that's good enough for me.

After you suggested the prototype by Marco Pivetta I went ahead and tried
it out. Worked without a problem and that's all I needed... BUT... I then
realized that throughout the app there are a ton of classes that type hint
a Zend Cache and giving them my own implementation would mean changing all
these classes. That would be my last option as these classes come from a
common repository that is used on other apps so changing dependencies would
mean breaking other apps. It *can* be done and would mean a lot of
rewriting but I'd rather do that as a last resort.

I didn't really understand Matthew's comment and I thought writing a plugin
would be as easy as what Marco was suggesting! :)

Another issue that I found is that on the ZF1 app we have an array in the
config where we could set the TTL for each different item (this array is
actually compiled with values from a DB). The home page alone has at least
3 different TTLs (this is an app for a site that serves a massive amount of
info with millions of hits per month). So tweaking these TTLs -which we do
quite often- means just changing a value in the DB via our CMS and that's
it. If I have different storage instances I need to change code to actually
change TTL values.

Do you see any way that this can be solved? I'm past an "elegant" solution
right now so anything that satisfies these requirements will work.

Thanks again for your precious non-baby time!







On Tue, Feb 11, 2014 at 3:05 PM, Marc Bennewitz <[hidden email]>wrote:

> Julian,
>
>
> Am 11.02.2014 18:41, schrieb Julian Vidal:
> > As mentioned in this pull
> > request<https://github.com/zendframework/zf2/pull/5386>ZF2 does not
> > provide per-item cache TTL like ZF1 did.
> That's not correct 100%.
> ZF2 cache storage adapter doesn't change TTL behavior of the storage
> itself.
> So if the storage itself (like memcached, apc, xcache, ...) supports it
> the ZF2 cache storage adapter supports it, too.
>
> > I'm using per-item TTL all throughout a ZF1 app which I'm currently
> > migrating to ZF2 and desperately need to be able to do this. If I need to
> > configure another cache adapter for every different TTL, my config file
> be
> > huge and very hard to maintain.
> The TTL is an option of a storage adapter as it's an option how the
> storage should handle items in it.
> If you have different TTL definitions normally you have different types
> of items which means you would need different configurations and storage
> instances.
> If you don't like that you need to write your own simple wrapper against
> a storage as @Ocramius prototyped here:
> https://github.com/zendframework/zf2/pull/5386#issuecomment-29381538
>
> > At some point in the discussion, Matthew Weier O'Phinney suggests:
> > "Implement a plugin to add Item based TTL for storages not supporting it
> > natively"
> >
> > Unfortunately, as I mention on my
> > comment<
> https://github.com/zendframework/zf2/pull/5386#issuecomment-34491161>,
> > I don't know how to write this kind of plugin. My deadline is approaching
> > so I will appreciate ANY pointers on this matter.
> As @weierophinney pointed out "... for storages not supporting it
> natively" - it's a great way of implementing it to make that behavior
> available for storages like the filesystem or dba. It doesn't magically
> change the API and allows a TTL argument ... -> it needs to fetch the
> configured TTL option, calculate the expected expired time in base of
> current time + TTL and store it together with the item. On reading it
> needs to check the stored expire time and return a "Item not found"
> behavior in cases it's expired.
> An implementation would look similar to the current serializer plugin.
>
> Currently I don't have enough time to do some more work I'm interested
> in as we born our (first) adorable daughter :)
>
> But I could assist :)
>
> > Thanks!
>
> Marc
>
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Per-item cache TTL

Marc Bennewitz


On 12.02.2014 00:50, Julian Vidal wrote:
> Marc,
>
> First of all, congratulations on your new baby. Say goodbye to your long
> night sleep for me :)
lol - thanks :)

>
> I'm not going to pretend I fully understand the discussion in the github PR
> as obviously there are very strong opinions on both sides. All I need is to
> make it work; and fast since I don't have the luxury of time for this
> particular issue. It seems to me that the discussion revolves around the
> responsibilities of the adapter; what it should (or not) do. I can't really
> support either side as I don't have the technical knowledge for that. All I
> can say is that the ZF1 way of doing this thing seemed much more flexible
> but if I can find a workaround that's good enough for me.
What ZF1 does is (if the backend doesn't support expiration natively) to
store the expiration time together with the item. On reading the time
will be checked against expired if no it returns the item data.

This procedure needs to be done by a storage plugin for ZF2.
(The TTL information needs to get retrieved from options)

>
> After you suggested the prototype by Marco Pivetta I went ahead and tried
> it out. Worked without a problem and that's all I needed... BUT... I then
> realized that throughout the app there are a ton of classes that type hint
> a Zend Cache and giving them my own implementation would mean changing all
> these classes. That would be my last option as these classes come from a
> common repository that is used on other apps so changing dependencies would
> mean breaking other apps. It *can* be done and would mean a lot of
> rewriting but I'd rather do that as a last resort.
Type-Hints against the StorageInterface should not be touched as if
function requires this interface it doesn't use it with you TTL
argument. You simply should pull the raw storage instance to such
functions - not the wrapped instance from your own.

>
> I didn't really understand Matthew's comment and I thought writing a plugin
> would be as easy as what Marco was suggesting! :)
see above - the plugin have to do different thins as the prototype
suggested by Marco.

>
> Another issue that I found is that on the ZF1 app we have an array in the
> config where we could set the TTL for each different item (this array is
> actually compiled with values from a DB). The home page alone has at least
> 3 different TTLs (this is an app for a site that serves a massive amount of
> info with millions of hits per month). So tweaking these TTLs -which we do
> quite often- means just changing a value in the DB via our CMS and that's
> it. If I have different storage instances I need to change code to actually
> change TTL values.
If I understand you currently you have 3 differnt TTL for three
different type of items - That's exactly what ZF2 cache storage is
designed for. Sure you will have three different instances but what is
the issue with it. Why you change TTLs in code - you have three
configuration sections each with TTL -> no code changes needed.

>
> Do you see any way that this can be solved? I'm past an "elegant" solution
> right now so anything that satisfies these requirements will work.
>
> Thanks again for your precious non-baby time!
It's in a baby sling right now :)

>
> On Tue, Feb 11, 2014 at 3:05 PM, Marc Bennewitz <[hidden email]>wrote:
>
>> Julian,
>>
>>
>> Am 11.02.2014 18:41, schrieb Julian Vidal:
>>> As mentioned in this pull
>>> request<https://github.com/zendframework/zf2/pull/5386>ZF2 does not
>>> provide per-item cache TTL like ZF1 did.
>> That's not correct 100%.
>> ZF2 cache storage adapter doesn't change TTL behavior of the storage
>> itself.
>> So if the storage itself (like memcached, apc, xcache, ...) supports it
>> the ZF2 cache storage adapter supports it, too.
>>
>>> I'm using per-item TTL all throughout a ZF1 app which I'm currently
>>> migrating to ZF2 and desperately need to be able to do this. If I need to
>>> configure another cache adapter for every different TTL, my config file
>> be
>>> huge and very hard to maintain.
>> The TTL is an option of a storage adapter as it's an option how the
>> storage should handle items in it.
>> If you have different TTL definitions normally you have different types
>> of items which means you would need different configurations and storage
>> instances.
>> If you don't like that you need to write your own simple wrapper against
>> a storage as @Ocramius prototyped here:
>> https://github.com/zendframework/zf2/pull/5386#issuecomment-29381538
>>
>>> At some point in the discussion, Matthew Weier O'Phinney suggests:
>>> "Implement a plugin to add Item based TTL for storages not supporting it
>>> natively"
>>>
>>> Unfortunately, as I mention on my
>>> comment<
>> https://github.com/zendframework/zf2/pull/5386#issuecomment-34491161>,
>>> I don't know how to write this kind of plugin. My deadline is approaching
>>> so I will appreciate ANY pointers on this matter.
>> As @weierophinney pointed out "... for storages not supporting it
>> natively" - it's a great way of implementing it to make that behavior
>> available for storages like the filesystem or dba. It doesn't magically
>> change the API and allows a TTL argument ... -> it needs to fetch the
>> configured TTL option, calculate the expected expired time in base of
>> current time + TTL and store it together with the item. On reading it
>> needs to check the stored expire time and return a "Item not found"
>> behavior in cases it's expired.
>> An implementation would look similar to the current serializer plugin.
>>
>> Currently I don't have enough time to do some more work I'm interested
>> in as we born our (first) adorable daughter :)
>>
>> But I could assist :)
>>
>>> Thanks!
>>
>> Marc
>>
>>
>> --
>> List: [hidden email]
>> Info: http://framework.zend.com/archives
>> Unsubscribe: [hidden email]
>>
>>
>>
>

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]