{"id":28717172,"url":"https://github.com/parminderpaul/lru-cache","last_synced_at":"2026-04-14T04:31:57.725Z","repository":{"id":298422109,"uuid":"999863428","full_name":"parminderpaul/lru-cache","owner":"parminderpaul","description":"This repository contains an object-oriented implementation of an LRU Cache in multiple programming languages. It demonstrates efficient `get` and `put` operations using a hashmap and doubly-linked-list structure. 🐙💻","archived":false,"fork":false,"pushed_at":"2025-07-09T08:01:50.000Z","size":68,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-09T09:22:46.157Z","etag":null,"topics":["arc","asyncio","cache","caching","cpp","data-structures","fifo-cache","go","header-only","in-memory","lfu","lfu-cache","lru","lru-cache","python","server","strapi","volatile"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":false,"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/parminderpaul.png","metadata":{"files":{"readme":"README.md","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":"2025-06-10T23:00:24.000Z","updated_at":"2025-07-09T08:01:54.000Z","dependencies_parsed_at":"2025-07-02T08:32:43.068Z","dependency_job_id":"51878177-ad5e-4941-a877-46efffaf4bfe","html_url":"https://github.com/parminderpaul/lru-cache","commit_stats":null,"previous_names":["parminderpaul/lru-cache"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/parminderpaul/lru-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parminderpaul%2Flru-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parminderpaul%2Flru-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parminderpaul%2Flru-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parminderpaul%2Flru-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parminderpaul","download_url":"https://codeload.github.com/parminderpaul/lru-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parminderpaul%2Flru-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31782736,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-14T02:24:21.117Z","status":"ssl_error","status_checked_at":"2026-04-14T02:24:20.627Z","response_time":153,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["arc","asyncio","cache","caching","cpp","data-structures","fifo-cache","go","header-only","in-memory","lfu","lfu-cache","lru","lru-cache","python","server","strapi","volatile"],"created_at":"2025-06-15T03:13:30.044Z","updated_at":"2026-04-14T04:31:57.705Z","avatar_url":"https://github.com/parminderpaul.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LRU Cache Implementations 🌐\r\n\r\nWelcome to the **LRU Cache** repository! This project provides demonstrative implementations of an LRU (Least Recently Used) cache in several programming languages. Whether you're preparing for an interview or just looking to deepen your understanding of caching algorithms, you've come to the right place.\r\n\r\n[![Download Releases](https://img.shields.io/badge/Download_Releases-Click_here-brightgreen)](https://github.com/parminderpaul/lru-cache/releases)\r\n\r\n## Table of Contents\r\n\r\n- [Introduction](#introduction)\r\n- [What is an LRU Cache?](#what-is-an-lru-cache)\r\n- [Why Use an LRU Cache?](#why-use-an-lru-cache)\r\n- [Implementations](#implementations)\r\n  - [Python](#python)\r\n  - [JavaScript](#javascript)\r\n  - [Java](#java)\r\n  - [C++](#c)\r\n  - [Go](#go)\r\n- [Usage](#usage)\r\n- [Contributing](#contributing)\r\n- [License](#license)\r\n- [Acknowledgments](#acknowledgments)\r\n\r\n## Introduction\r\n\r\nCaching is a crucial concept in computer science. An LRU cache is a data structure that stores a limited number of items. When the cache reaches its limit, it removes the least recently used item to make space for new data. This repository demonstrates various implementations of the LRU cache in different programming languages, making it a great resource for both learning and practice.\r\n\r\n## What is an LRU Cache?\r\n\r\nAn LRU cache is a type of cache that discards the least recently used items first. It keeps track of the order in which items are accessed. When the cache is full, the item that has not been used for the longest time is removed. This approach helps optimize memory usage and speeds up data retrieval.\r\n\r\n### Key Characteristics\r\n\r\n- **Limited Size**: The cache has a fixed size.\r\n- **Efficient Access**: It allows for quick access to recently used items.\r\n- **Eviction Policy**: It uses the least recently used policy for item removal.\r\n\r\n## Why Use an LRU Cache?\r\n\r\nUsing an LRU cache can significantly improve performance in applications where data retrieval speed is critical. Here are some scenarios where an LRU cache can be beneficial:\r\n\r\n- **Web Browsers**: Caching web pages for faster access.\r\n- **Database Systems**: Storing frequently accessed data.\r\n- **API Calls**: Reducing the number of calls to external services.\r\n\r\n## Implementations\r\n\r\nThis repository includes LRU cache implementations in various programming languages. Each implementation is designed to showcase the core principles of the LRU caching mechanism.\r\n\r\n### Python\r\n\r\n```python\r\nclass LRUCache:\r\n    def __init__(self, capacity: int):\r\n        self.cache = {}\r\n        self.capacity = capacity\r\n        self.order = []\r\n\r\n    def get(self, key: int) -\u003e int:\r\n        if key in self.cache:\r\n            self.order.remove(key)\r\n            self.order.append(key)\r\n            return self.cache[key]\r\n        return -1\r\n\r\n    def put(self, key: int, value: int) -\u003e None:\r\n        if key in self.cache:\r\n            self.order.remove(key)\r\n        elif len(self.cache) \u003e= self.capacity:\r\n            lru = self.order.pop(0)\r\n            del self.cache[lru]\r\n        self.cache[key] = value\r\n        self.order.append(key)\r\n```\r\n\r\n### JavaScript\r\n\r\n```javascript\r\nclass LRUCache {\r\n    constructor(capacity) {\r\n        this.capacity = capacity;\r\n        this.cache = new Map();\r\n    }\r\n\r\n    get(key) {\r\n        if (!this.cache.has(key)) return -1;\r\n        const value = this.cache.get(key);\r\n        this.cache.delete(key);\r\n        this.cache.set(key, value);\r\n        return value;\r\n    }\r\n\r\n    put(key, value) {\r\n        if (this.cache.has(key)) {\r\n            this.cache.delete(key);\r\n        } else if (this.cache.size \u003e= this.capacity) {\r\n            this.cache.delete(this.cache.keys().next().value);\r\n        }\r\n        this.cache.set(key, value);\r\n    }\r\n}\r\n```\r\n\r\n### Java\r\n\r\n```java\r\nimport java.util.LinkedHashMap;\r\nimport java.util.Map;\r\n\r\npublic class LRUCache extends LinkedHashMap\u003cInteger, Integer\u003e {\r\n    private final int capacity;\r\n\r\n    public LRUCache(int capacity) {\r\n        super(capacity, 0.75f, true);\r\n        this.capacity = capacity;\r\n    }\r\n\r\n    public int get(int key) {\r\n        return super.getOrDefault(key, -1);\r\n    }\r\n\r\n    public void put(int key, int value) {\r\n        super.put(key, value);\r\n        if (size() \u003e capacity) {\r\n            remove(entrySet().iterator().next().getKey());\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n### C++\r\n\r\n```cpp\r\n#include \u003cunordered_map\u003e\r\n#include \u003clist\u003e\r\n\r\nclass LRUCache {\r\npublic:\r\n    LRUCache(int capacity) : capacity(capacity) {}\r\n\r\n    int get(int key) {\r\n        if (cacheMap.find(key) == cacheMap.end()) {\r\n            return -1;\r\n        }\r\n        cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);\r\n        return cacheMap[key]-\u003esecond;\r\n    }\r\n\r\n    void put(int key, int value) {\r\n        if (cacheMap.find(key) != cacheMap.end()) {\r\n            cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);\r\n            cacheMap[key]-\u003esecond = value;\r\n            return;\r\n        }\r\n        if (cacheList.size() == capacity) {\r\n            auto last = cacheList.back();\r\n            cacheMap.erase(last.first);\r\n            cacheList.pop_back();\r\n        }\r\n        cacheList.emplace_front(key, value);\r\n        cacheMap[key] = cacheList.begin();\r\n    }\r\n\r\nprivate:\r\n    int capacity;\r\n    std::list\u003cstd::pair\u003cint, int\u003e\u003e cacheList;\r\n    std::unordered_map\u003cint, decltype(cacheList.begin())\u003e cacheMap;\r\n};\r\n```\r\n\r\n### Go\r\n\r\n```go\r\npackage main\r\n\r\nimport \"container/list\"\r\n\r\ntype LRUCache struct {\r\n    capacity int\r\n    cache    map[int]*list.Element\r\n    order    *list.List\r\n}\r\n\r\ntype entry struct {\r\n    key   int\r\n    value int\r\n}\r\n\r\nfunc Constructor(capacity int) LRUCache {\r\n    return LRUCache{\r\n        capacity: capacity,\r\n        cache:    make(map[int]*list.Element),\r\n        order:    list.New(),\r\n    }\r\n}\r\n\r\nfunc (this *LRUCache) Get(key int) int {\r\n    if elem, found := this.cache[key]; found {\r\n        this.order.MoveToFront(elem)\r\n        return elem.Value.(entry).value\r\n    }\r\n    return -1\r\n}\r\n\r\nfunc (this *LRUCache) Put(key int, value int) {\r\n    if elem, found := this.cache[key]; found {\r\n        this.order.MoveToFront(elem)\r\n        elem.Value = entry{key, value}\r\n    } else {\r\n        if this.order.Len() == this.capacity {\r\n            back := this.order.Back()\r\n            this.order.Remove(back)\r\n            delete(this.cache, back.Value.(entry).key)\r\n        }\r\n        newElem := this.order.PushFront(entry{key, value})\r\n        this.cache[key] = newElem\r\n    }\r\n}\r\n```\r\n\r\n## Usage\r\n\r\nTo use the LRU cache implementations, follow these steps:\r\n\r\n1. **Clone the Repository**:\r\n   Clone this repository to your local machine using:\r\n   ```bash\r\n   git clone https://github.com/parminderpaul/lru-cache.git\r\n   ```\r\n\r\n2. **Choose Your Language**:\r\n   Navigate to the directory of the language you are interested in.\r\n\r\n3. **Run the Code**:\r\n   Execute the code using the appropriate interpreter or compiler for the chosen language.\r\n\r\n4. **Explore the Examples**:\r\n   Each implementation includes example usage. Feel free to modify the examples to test different scenarios.\r\n\r\n## Contributing\r\n\r\nContributions are welcome! If you want to add more implementations or improve existing ones, please follow these steps:\r\n\r\n1. Fork the repository.\r\n2. Create a new branch (`git checkout -b feature/YourFeature`).\r\n3. Make your changes and commit them (`git commit -m 'Add new feature'`).\r\n4. Push to the branch (`git push origin feature/YourFeature`).\r\n5. Create a pull request.\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\r\n\r\n## Acknowledgments\r\n\r\n- Thanks to the open-source community for their contributions and support.\r\n- Special thanks to those who have shared their knowledge about caching algorithms.\r\n\r\nFor more information and to download the latest releases, visit [Releases](https://github.com/parminderpaul/lru-cache/releases).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparminderpaul%2Flru-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparminderpaul%2Flru-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparminderpaul%2Flru-cache/lists"}