Promote horizon.utils.memoized to Oslo

Registered by Shawn Hartsock

This blueprint is to promote code from Horizon to Oslo for use across OpenStack projects.

http://docs.openstack.org/developer/horizon/sourcecode/horizon/horizon.utils.memoized.html

I have seen a number of patches where people have begun to implement
their own caches in dictionaries. This typically confuses the code and
mixes issues of correctness and performance in code.

Here's an example:

We start with:

    def my_thing_method(some_args):
        # do expensive work
        return value

... but a performance problem is detected... maybe the method is
called 15 times in 10 seconds but then not again for 5 minutes and the
return value can only logically change every minute or two... so we
end up with ...

 _GLOBAL_THING_CACHE = {}

 def my_thing_method(some_args):
    key = key_from(some_args)
     if key in _GLOBAL_THING_CACHE:
         return _GLOBAL_THING_CACHE[key]
     else:
          # do expensive work
          _GLOBAL_THING_CACHE[key] = value
          return value

... which is all well and good... but now as a maintenance programmer
I need to comprehend the cache mechanism, when cached values are
invalidated, and if I need to debug the "do expensive work" part I
need to tease out some test that prevents the cache from being hit.
Plus I've introduced a new global variable. We love globals right?

I would like us to be able to say:

@memoized
def my_thing_method(some_args):
    # do expensive work
    return value

... in Nova, Cinder, or Swift ... just as they can in Horizon.

Blueprint information

Status:
Started
Approver:
Doug Hellmann
Priority:
Low
Drafter:
Shawn Hartsock
Direction:
Needs approval
Assignee:
Shawn Hartsock
Definition:
Approved
Series goal:
None
Implementation:
Good progress
Milestone target:
None
Started by
Shawn Hartsock

Related branches

Sprints

Whiteboard

Note:
see also
* https://github.com/openstack/nova/blob/master/nova/api/ec2/ec2utils.py#L40

Code at:
    https://review.openstack.org/#/c/71087/

TODO: the memoized.py did not appear to have any matching testing associated with it. I will write some unit tests to cover basic use before removing the WIP tag on the patch.

TODO:
Based on feeback, I am attempting to break this into an alternative cache backend and a decorator.

----

How is this substantially different from https://review.openstack.org/#/c/72291/ ? -- dhellmann

Other than the backing, that is the same basic idea.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.