#12941: Cache should be cleared on cloning
----------------------------------------+-----------------------------------
Reporter: hivert | Owner: hivert
Type: defect | Status: new
Priority: major | Milestone: sage-5.1
Component: combinatorics | Resolution:
Keywords: Clone, cache, days38 | Work issues:
Report Upstream: N/A | Reviewers:
Authors: Florent Hivert | Merged in:
Dependencies: | Stopgaps:
----------------------------------------+-----------------------------------
Comment (by SimonKing):
Here is a second potential solution: !ClearCacheOnPickle, that existed
prior to #11115, but was totally broken. Now it works, as follows.
You need to define some class with cached methods:
{{{
#!python
from sage.misc.cachefunc import CachedMethodCaller,
CachedMethodCallerNoArgs, cached_method
class B(object):
def __init__(self,x):
self.x = x
def __getstate__(self):
return (self.__dict__,)
def __setstate__(self,L):
self.__dict__ = L[0]
@cached_method
def f(self,i):
return self.x+i
@cached_method
def g(self):
return self.x*2
}}}
Then, you derive a new class from it that also inherits from
!ClearCacheOnPickle; note that it is important that B has (or inherits) a
`__getstate__` method, because !ClearCacheOnPickle calls it when pickling:
{{{
#!python
from sage.misc.cachefunc import ClearCacheOnPickle
class A(ClearCacheOnPickle,B):
@cached_method
def h(self,i):
return self.x**i
}}}
Note that the derived class has an additional cached method. Let us see
what happens:
{{{
sage: a = A(5)
sage: a.f(7)
12
sage: x = a.f(7)
sage: a.g()
10
sage: a.h(7)
78125
sage: b = copy(a)
sage: b.x = 7
sage: b.f(7)
14
sage: b.g()
14
sage: b.h(7)
823543
sage: x is a.f(7)
True
}}}
So, all cached methods have automatically been cleared (but only in the
copy, not in the original instance).
I don't know if it makes sense to use !ClearCacheOnPickle in your setting,
but perhaps it helps.
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12941#comment:12>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.