473,796 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimize a cache

Hello,
I am building a object cache in python, The cache has a maximum size and the
items have expiration dates.
At the moment I'm doing like that:

cache = {} # create dictionary

cache[ID] = (object, timestamp) # Save a tuple with the object itself and a
timestamp (from datetime.dateti me.now()) under a unique object ID. This
make looking up a certain item a cheap operation (AFAIK). After looking up
my program checks if the expiration date and returns cache[ID][1] or
retrieve a new object and overwrite the one saved in the cache.

My problem now:
When len(cache) > cacheSize I need to remove the oldest objects (the ones
with the smalest timestamp). For that I need to iterate through all items
and get the oldest one: (pseudo code)

oldest = datetime.dateti me.now()

for i in cache:
if i[1] < a:
id = i
n = i[0]

del cache[id]
What possible you see to optimize this lookup? Or anything else you see to
make it better?

Thanks,

Florian
Jul 19 '05 #1
5 1516
Florian Lindner <Fl************ *@xgm.de> writes:
What possible you see to optimize this lookup? Or anything else you see to
make it better?


Use the heapq module.
Jul 19 '05 #2
Florian Lindner <Fl************ *@xgm.de> writes:
What possible you see to optimize this lookup? Or anything else you see to
make it better?


Do you need to update the timestamp of cached entries when you access
them? If yes, use the heapq module. If no, is the cache something
different from a simple FIFO?
Jul 19 '05 #4
----- Original Message -----
From: "Florian Lindner" <Fl************ *@xgm.de>

Hello,
I am building a object cache in python, The cache has a maximum size and the items have expiration dates.
At the moment I'm doing like that: What possible you see to optimize this lookup? Or anything else you see to
make it better?


Have a look at http://py.vaults.ca/apyllo.py/514463...44789.92554878
it already has some of your requirements, and can be used as a building
block.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2185
by: Jason Smith | last post by:
Hi. I just have a question about optimizations Python does when converting to bytecode. import re for someString in someListOfStrings: if re.match('foo', someString): print someString, "matched!" Does Python notice that re.match is called with the same expression, and thus lift it out of the loop? Or do I need to always optimize by hand
0
1798
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for "optimize table", but (seemingly) the server version 4.0.16 requires INSERT as well. Is the INSERT privelege necessary for performing optimize in mysql
9
1471
by: Julie | last post by:
Due to circumstances beyond my immediate control, I've been put in charge of getting some JavaScript code in place and operational ASAP. Normally, I'm a C++ programmer, and previously have only barely fiddled w/ JS. The good news is that I have been able to get through the JS coding and everything is working as expected; the bad news is that it doesn't perform as well as I'd like. I feel that the performance problems that I'm...
0
2018
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an option in there to optimize the code, has anyone had any expeiance / problems with this option and were there any significant gains in performance by turning on this setting?
1
1326
by: Sachi | last post by:
Hi, Is there any best choice to eliminate the usage or reduce the usage of session object when doing paging in datagrid? Thanks in advance, Sachi
1
1529
by: TCB | last post by:
I am developing a website using .NET 2.0 (classified style website) it displays a series of records per page (8, using a datagrid with paging) each record displays a thumbnail of an image that sizes about 6-7k and a small text description of the item. The Items tamble holds now about 2500 hundred records so that´s 60+ pages. The thing is that all the page when it load is about 175K. Of those 83K are from images the rest is the text that...
1
2512
by: peter.fredrikson | last post by:
Hi, I have a web server application (C#) which I would like to speed up a little. I would appriciate a discussion of design principles for a specific optimization scenario described below. My web server process every request by building a XML structure containing both application data, data from the database (or another system), and presentation data, describing how to present the application data in the GUI. Then the web server...
5
2527
by: timor.super | last post by:
Hi group, imagine I want to find number of values of a word in a string ... Look at my code : List<stringthingsToFind = new List<string>(new string {"error", "values"}); MyClass myClass = new MyClass(thingsToFind, data); foreach (KeyValuePair<int, stringaValue in
4
3298
by: legrape | last post by:
I am porting a piece of C code to 64bit on Linux. I am using 64bit integers. It is a floating point intensive code and when I compile (gcc) on 64 bit machine, I don't see any runtime improvement when optimizing -O3. If I construct a small program I can get significant (>4x) speed improvement using -O3 versus -g. If I compile on a 32 bit machine, it runs 5x faster on the 64 bit machine than does the 64bit compiled code. It seems like...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10244
jinu1996
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...
1
10201
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,...
0
10021
tracyyun
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...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5454
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...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.