GithubHelp home page GithubHelp logo

isabella232 / lru-dict Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yougov/lru-dict

0.0 0.0 0.0 97 KB

A C extension of a python dict-like LRU container.

License: MIT License

C 69.90% Python 30.10%

lru-dict's Introduction

LRU Dict

A fixed size dict like container which evicts Least Recently Used (LRU) items once size limit is exceeded. There are many python implementations available which does similar things. This is a fast and efficient C implementation. LRU maximum capacity can be modified at run-time. If you are looking for pure python version, look else where.

Usage

This can be used to build a LRU cache. Usage is almost like a dict.

from lru import LRU
l = LRU(5)         # Create an LRU container that can hold 5 items
for i in range(5):
   l[i] = str(i)
print l.items()    # Prints items in MRU order
# Would print [(4, '4'), (3, '3'), (2, '2'), (1, '1'), (0, '0')]

l[5] = '5'         # Inserting one more item should evict the old item
print l.items()
# Would print [(5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')]

l[3]               # Accessing an item would make it MRU
print l.items()
# Would print [(3, '3'), (5, '5'), (4, '4'), (2, '2'), (1, '1')]
# Now 3 is in front

l.keys()           # Can get keys alone in MRU order
# Would print [3, 5, 4, 2, 1]

del l[4]           # Delete an item
print l.items()
# Would print [(3, '3'), (5, '5'), (2, '2'), (1, '1')]

print l.get_size()
#Would print 5
l.set_size(3)
print l.items()
# Would print [(3, '3'), (5, '5'), (2, '2')]
print l.get_size()
# Would print 3

l.get_hits()
l.get_misses()

l.clear()
print l.items()
#Would print []

Install

pip install lru-dict

or

easy_install lru_dict

When to use this

Like mentioned above there are many python implementations of an LRU. Use this if you need a faster and memory efficient alternative. It is implemented with a dict and associated linked list to keep track of LRU order. See code for a more detailed explanation. To see an indicative comparison with a pure python module, consider a benchmark against pylru (just chosen at random, it should be similar with other python implementations as well).

$ python bench.py pylru.lrucache
Time : 3.31 s, Memory : 453672 Kb
$ python bench.py lru.LRU
Time : 0.23 s, Memory : 124328 Kb

lru-dict's People

Contributors

amitdev avatar bdeeney avatar milonline-eu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.