{"id":27257812,"url":"https://github.com/amitdev/lru-dict","last_synced_at":"2025-05-15T06:02:20.005Z","repository":{"id":8660124,"uuid":"10314393","full_name":"amitdev/lru-dict","owner":"amitdev","description":"A fast and memory efficient LRU cache for Python","archived":false,"fork":false,"pushed_at":"2025-04-15T04:17:52.000Z","size":100,"stargazers_count":275,"open_issues_count":18,"forks_count":49,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-15T06:02:11.940Z","etag":null,"topics":["python"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amitdev.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-05-27T12:29:37.000Z","updated_at":"2025-05-14T14:20:56.000Z","dependencies_parsed_at":"2023-02-10T11:01:23.217Z","dependency_job_id":"3612cb73-460f-4d5a-ad8a-0b82c2ca9f27","html_url":"https://github.com/amitdev/lru-dict","commit_stats":{"total_commits":74,"total_committers":14,"mean_commits":5.285714285714286,"dds":0.5810810810810811,"last_synced_commit":"5887dd68e6a5d2285ea0efb61d74961b6f2e1384"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitdev%2Flru-dict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitdev%2Flru-dict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitdev%2Flru-dict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitdev%2Flru-dict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amitdev","download_url":"https://codeload.github.com/amitdev/lru-dict/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254283336,"owners_count":22045140,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python"],"created_at":"2025-04-11T03:18:38.987Z","updated_at":"2025-05-15T06:02:19.940Z","avatar_url":"https://github.com/amitdev.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":".. image:: https://github.com/amitdev/lru-dict/actions/workflows/tests.yml/badge.svg\n    :target: https://github.com/amitdev/lru-dict/actions/workflows/tests.yml\n\n.. image:: https://github.com/amitdev/lru-dict/actions/workflows/build-and-deploy.yml/badge.svg\n    :target: https://github.com/amitdev/lru-dict/actions/workflows/build-and-deploy.yml\n\nLRU Dict\n========\n\nA fixed size dict like container which evicts Least Recently Used (LRU) items\nonce size limit is exceeded. There are many python implementations available\nwhich does similar things. This is a fast and efficient C implementation.\nLRU maximum capacity can be modified at run-time.\nIf you are looking for pure python version, look `else where \u003chttp://www.google.com/search?q=python+lru+dict\u003e`_.\n\nUsage\n=====\n\nThis can be used to build a LRU cache. Usage is almost like a dict.\n\n.. code:: python3\n\n  from lru import LRU\n  l = LRU(5)         # Create an LRU container that can hold 5 items\n\n  print l.peek_first_item(), l.peek_last_item()  #return the MRU key and LRU key\n  # Would print None None\n\n  for i in range(5):\n     l[i] = str(i)\n  print l.items()    # Prints items in MRU order\n  # Would print [(4, '4'), (3, '3'), (2, '2'), (1, '1'), (0, '0')]\n\n  print l.peek_first_item(), l.peek_last_item()  #return the MRU key and LRU key\n  # Would print (4, '4') (0, '0')\n\n  l[5] = '5'         # Inserting one more item should evict the old item\n  print l.items()\n  # Would print [(5, '5'), (4, '4'), (3, '3'), (2, '2'), (1, '1')]\n\n  l[3]               # Accessing an item would make it MRU\n  print l.items()\n  # Would print [(3, '3'), (5, '5'), (4, '4'), (2, '2'), (1, '1')]\n  # Now 3 is in front\n\n  l.keys()           # Can get keys alone in MRU order\n  # Would print [3, 5, 4, 2, 1]\n\n  del l[4]           # Delete an item\n  print l.items()\n  # Would print [(3, '3'), (5, '5'), (2, '2'), (1, '1')]\n\n  print l.get_size()\n  # Would print 5\n\n  l.set_size(3)\n  print l.items()\n  # Would print [(3, '3'), (5, '5'), (2, '2')]\n  print l.get_size()\n  # Would print 3\n  print l.has_key(5)\n  # Would print True\n  print 2 in l\n  # Would print True\n\n  l.get_stats()\n  # Would print (1, 0)\n\n\n  l.update(5='0')           # Update an item\n  print l.items()\n  # Would print [(5, '0'), (3, '3'), (2, '2')]\n\n  l.clear()\n  print l.items()\n  # Would print []\n\n  def evicted(key, value):\n    print \"removing: %s, %s\" % (key, value)\n\n  l = LRU(1, callback=evicted)\n\n  l[1] = '1'\n  l[2] = '2'\n  # callback would print removing: 1, 1\n\n  l[2] = '3'\n  # doesn't call the evicted callback\n\n  print l.items()\n  # would print [(2, '3')]\n  \n  del l[2]\n  # doesn't call the evicted callback\n\n  print l.items()\n  # would print []\n\nInstall\n=======\n\n::\n\n  pip install lru-dict\n\nor\n\n::\n\n  easy_install lru_dict\n\n\nWhen to use this\n================\n\nLike mentioned above there are many python implementations of an LRU. Use this\nif you need a faster and memory efficient alternative. It is implemented with a\ndict and associated linked list to keep track of LRU order. See code for a more\ndetailed explanation. To see an indicative comparison with a pure python module,\nconsider a `benchmark \u003chttps://gist.github.com/amitdev/5773979\u003e`_ against\n`pylru \u003chttps://pypi.python.org/pypi/pylru/\u003e`_ (just chosen at random, it should\nbe similar with other python implementations as well).\n\n::\n\n  $ python bench.py pylru.lrucache\n  Time : 3.31 s, Memory : 453672 Kb\n  $ python bench.py lru.LRU\n  Time : 0.23 s, Memory : 124328 Kb\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famitdev%2Flru-dict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famitdev%2Flru-dict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famitdev%2Flru-dict/lists"}