In the code below, the class DifferentCache utilizes three different
memoization (caching) strategies. Neither the function Memoize1 or
the class Memoize2 will be adequate for all three of these cases (I
intend these to be used as, for example,
getInstanceValu eFunction = Memoize1(getIns tanceValueFunct ion)
within the DifferentCache class definition).
Memoize1 will have problems with getMemberValueF unction b/c it
will try to generate a cache key for (self, 'arg1', 'arg2') whereas
the actual values only depend on ('val1', 'val2') (though this may be
more of a nuisance then an error).
Memoize2 will have problems with getInstanceValu eFunction b/c
instantiating Memoize2 will cause 'self' to refer to the Memoize2
object and not to the DifferentCache object when computing the desired
function (and worse yet, since the self that does reference
DifferentCache is bound to the DifferentCache. getInstanceValu eFunction
it is not even passed in as an argument when Memoize2.__call __ is
executed .... apologies if my terminology is off). Actually, would
this problem apply to any use of Memoize2 to any instance method?
Also, for any of these memoizations, there is only 1 self so we really
don't need it as a cache key.
Both Memoize methods will have problems, in addition, b/c the hashing
for an object is based on __str__ which should also be Memoized --
that is, there is a circular dependency of __hash__ on __str__ and of
__str__ on __hash__. Perhaps a generator that computed the value the first
time and then subsequently return that value from a stored variable?
Obviously, you could deal with these problems by simply keeping each
individual cacheing strategy for the different methods. However, can
anyone see how to unify these into a common cacheing mechanism? Or,
can it be handled by some careful rewriting of how the functions
(methods) are called?
#
# from ?? on comp.lang.pytho n
#
def Memoize1(func):
cache = {}
def _internal(*args ):
if cache.has_key(a rgs):
return cache[args]
else:
ans = cache[args] = func(*args)
return ans
return _internal
#
# from Peter norvig on comp.lang.pytho n
#
class Memoize2:
def __init__(self, fn):
self.cache={}
self.fn=fn
def __call__(self,* args):
if self.cache.has_ key(args):
return self.cache[args]
else:
object = self.cache[args] = self.fn(*args)
return object
#
# an example class
#
class DifferentCache:
def __init__(self, value):
self.value = value
self.complexVal ueCache = {}
def getInstanceValu eFunction(self) :
try:
value = self.instanceVa lueCache
except KeyError:
value = self.instanceVa lueCache = someFunction(se lf)
return value
def getMemberValueF unction(self, other1, other2):
try:
value = self.complexVal ueCache[other]
except KeyError:
value = self.complexVal ueCache[other] = \
someOtherFuncti on(self, other1, other2))
return value
def __str__(self):
try:
strValue = self.stringCach e
except AttributeError:
strValue = self.stringCach e = str(self.value)
return strValue
def nonCachedFuncti on(self):
return yetAnotherFunct ion(self)
def __hash__(self):
return str.__hash__(st r(self))
#
# Desired rewrite or so
#
class DifferentCache:
def __init__(self, value):
self.value = value
@Memoized
def getInstanceValu eFunction(self) :
return someFunction(se lf)
@Memoized
def getMemberValueF unction(self, other1, other2):
return someOtherFuncti on(self, other1, other2)
@Memoized
def __str__(self):
return str(self.value)
def nonCachedFuncti on(self):
return yetAnotherFunct ion(self)
def __hash__(self):
return str.__hash__(st r(self))
Note, the various functions within the instance methods are meant to
represent some arbitrary code executed on and computing values from the
various arguments. 0 1376 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: J. Baute |
last post by:
I'm caching data in the Application object to speed up certain pages on a website
The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data from the "cache" hardly takes any time
A basic pattern used to get the data from disk, or from cache is this
data = getDataFromCache("mydata"
if data = "" the
data = getDataFromDisk(
storeDataInCache(data, "mydata"
end i
|
by: Bob Rock |
last post by:
Hello,
I'd like to have some suggestions/pointers to how I could implement caching
of generic data used by a set of web methods. I don't need to cache the
response of web methods but of generic data (in the form of key/value
pairs).
..
Based on my current knowledge I have the following two choices:
1) using the HttpApplication class
|
by: olle |
last post by:
Hi folks.
I learning asp.net and compare it with traditional asp and
Access-developing.
The issue is this one:
1/I have this Ms Acceess adp-project application that works fine on my
Ms Sql server database.
In my main form I have an Access-combobox with Customer-names from my
customer table.
In this combo-box are about 2000 records.
|
by: Gavin Pollock |
last post by:
Is anyone using Caching (HttpRuntime.Cache) in Whidbey? Not sure if there's
another newsgroup for this though since it's still beta....
I'm having issues running a system built on 1.1 in a 2.0 environment...
Simple (I think!!) use of the Cache as below,
BOSContext bosContext = new BOSContext();
StringBuilder Html = new StringBuilder();
|
by: George1776 |
last post by:
All,
I've recently upgraded our production ASP.NET/C# application from
framework 1.1 to 2.0. Since then I've been plagued by out-of-memory
errors and problems with the cache object (which may simply be a result
of being out of memory.)
We're running on IIS 5.1 on a single Windows 2000 server. We have a
separate database server - SQL Server 2000 64 bit. Session state is
stored on the database.
| |
by: Henrik Dahl |
last post by:
Hello!
In my application I have a need for using a regular expression now and then.
Often the same regular expression must be used multiple times. For
performance reasons I use the RegexOptions.Compiled when I instantiate it.
It must be obvious that it takes some time to instantiate such an object.
My question is, does the Regex instantiation somehow deal with some caching
internally so instantiating a Regex object multiple times...
|
by: mark4asp |
last post by:
How can I prevent Caching of JavaScript and CSS files ONLY when I
deploy a new application? I only want to force a refresh the first
time the client uses the new build.
For instance, I'm told I can do it with javascript by including a
version number as a querystring like this:
<script type="text/javascript" src="../javascript/menu.js?v=2"></
script>
However that won't solve the problem of the css files.
|
by: trss |
last post by:
Has anyone experienced automatic memoization by any C++ compiler
before?
The program coded as a solution for the problem based on the famous 3n
+1 problem, both of which are given below, is automatically memoized.
Is it due to caching of return values or something else? The point is,
does this program exhibit some property which leads to automatic
memoization by the compiler? Which can be satisfied by other programs
too to make them...
|
by: ssecorp |
last post by:
I am not clear about the results here.
from timeit import Timer
import Decorators
def fib(n):
a, b = 1, 0
while n:
a, b, n = b, a+b, n-1
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |