{"id":22656537,"url":"https://github.com/jimmyyyeh/flask-api-cache","last_synced_at":"2026-05-08T15:18:57.073Z","repository":{"id":57430090,"uuid":"301976453","full_name":"jimmyyyeh/flask-api-cache","owner":"jimmyyyeh","description":"A package for caching flask api with custom key.","archived":false,"fork":false,"pushed_at":"2021-04-01T07:28:03.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T23:27:34.362Z","etag":null,"topics":["flask","python","redis","restful-api"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/flask-api-cache/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jimmyyyeh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-07T08:44:10.000Z","updated_at":"2024-08-06T09:28:55.000Z","dependencies_parsed_at":"2022-08-26T03:25:21.456Z","dependency_job_id":null,"html_url":"https://github.com/jimmyyyeh/flask-api-cache","commit_stats":null,"previous_names":["chienfeng0719/flask-api-cache"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/jimmyyyeh/flask-api-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmyyyeh%2Fflask-api-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmyyyeh%2Fflask-api-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmyyyeh%2Fflask-api-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmyyyeh%2Fflask-api-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jimmyyyeh","download_url":"https://codeload.github.com/jimmyyyeh/flask-api-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jimmyyyeh%2Fflask-api-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266516347,"owners_count":23941401,"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","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"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":["flask","python","redis","restful-api"],"created_at":"2024-12-09T10:14:56.489Z","updated_at":"2026-05-08T15:18:51.981Z","avatar_url":"https://github.com/jimmyyyeh.png","language":"Python","funding_links":["https://www.buymeacoffee.com/jimmyyyeh"],"categories":[],"sub_categories":[],"readme":"# flask-api-cache\n**A package for caching flask api with custom key.**\n\n\n## Description:\nA decorator for python flask api.\n\nYou can set cache in your **memory** or with **redis instance**,\u003cbr\u003e\nthe key will be generated by the following rule:\u003cbr\u003e\n`{REQUEST_FULL_PATH}:{PARAMETER_STRING}`\u003cbr\u003e\nor you can use your custom key function by key_func args,\u003cbr\u003e\nthe value will be your function return value.\u003cbr\u003e\n\n## How To Use:\n\n### Import\n```python\nfrom flask_api_cache import ApiCache\n```\n\n### Parameters\n\n|name|required|description|\n|----|--------|-----------|\n|redis|no|if you want to caching data in redis, you can call ApiCache with a redis instance.|\n|expired_time|no|set your expired time with **seconds**, the default value is 24 * 60 * 60 seconds (1 day)|\n|key_func|no|the function which you want to make custom key|\n\n### Cache Without Redis\n```python\n@app.route('/example_1/\u003cstring:name\u003e')\n@ApiCache(expired_time=10)\ndef example_1(name):\n    \"\"\"\n    caching data in memory with default key.\n        - http://0.0.0.0:5000/example_1/jimmy\n    :param name:\n    :return:\n    \"\"\"\n    return jsonify(f'Hi {name}, it is {datetime.now()}')\n```\nIf you request for **http://0.0.0.0:5000/example_1/jimmy**,\u003cbr\u003e\nit will set a 10 seconds cache by key: `example_1:/example_1/Jimmy?`,\u003cbr\u003e\n                     with value: `Hi jimmy, it is 2020-10-23 16:06:27.996358.`,\nin your memory, it will be cleared after api service restart.\n\n### Cache With Redis\n```python\n@app.route('/example_3/\u003cstring:items\u003e')\n@ApiCache(redis=redis_instance, expired_time=10)\ndef example_3(items):\n    \"\"\"\n    caching data in redis instance with default key.\n        - http://0.0.0.0:5000/example_3/coffee\n    :param items:\n    :return:\n    \"\"\"\n    return jsonify(f'You bought {items} at {datetime.now()}')\n```\nIf you request for **http://0.0.0.0:5000/example_3/coffee**,\u003cbr\u003e\nit will set a 10 seconds cache by key: `example_3:/example_3/coffee?`,\u003cbr\u003e\n                     with value: `You bought coffee at 2020-10-23 16:06:59.904216`,\nin your redis instance.\n\n### Cache With Custom Function\n```python\ndef custom_func_2(**kwargs):\n    items = kwargs.get('items')\n    price = kwargs.get('price')\n    keys = f'{items}:{price}'\n    return keys\n\n@app.route('/example_4/\u003cstring:items\u003e/\u003cint:price\u003e')\n@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)\ndef example_4(items, price):\n    \"\"\"\n    caching data in redis instance with custom function.\n        - http://0.0.0.0:5000/example_4/coffee/20\n    :param items:\n    :param price:\n    :return:\n    \"\"\"\n    return jsonify(f'You bought {items} at {datetime.now()}, it cost ${price}')\n\n```\nIf you request for **http://0.0.0.0:5000/example_4/coffee/20** ,\u003cbr\u003e\nit will set a 10 seconds cache by key: `coffee:20`,\u003cbr\u003e\n                     with value: `You bought coffee at 2020-10-23 16:07:59.818357, it cost $20`,\nin your memory, it will be cleared after service restart.\n\n### [Sample Code](https://github.com/chienfeng0719/flask-api-cache/blob/develop/example.py)\n```python\n# -*- coding: utf-8 -*-\n\"\"\"\n      ┏┓       ┏┓\n    ┏━┛┻━━━━━━━┛┻━┓\n    ┃      ☃      ┃\n    ┃  ┳┛     ┗┳  ┃\n    ┃      ┻      ┃\n    ┗━┓         ┏━┛\n      ┗┳        ┗━┓\n       ┃          ┣┓\n       ┃          ┏┛\n       ┗┓┓┏━━━━┳┓┏┛\n        ┃┫┫    ┃┫┫\n        ┗┻┛    ┗┻┛\n    God Bless,Never Bug\n\"\"\"\nimport redis\nfrom flask import Flask, request, jsonify\nfrom flask_api_cache import ApiCache\nfrom datetime import datetime\n\napp = Flask(__name__)\n\nredis_instance = redis.StrictRedis(host='redis', port=6379)\n\n\ndef custom_func_1(**kwargs):\n    name = kwargs.get('name')\n    age = kwargs.get('age')\n    sex = kwargs.get('sex')\n    keys = f'{name}:{age}:{sex}'\n    return keys\n\n\ndef custom_func_2(**kwargs):\n    items = kwargs.get('items')\n    price = kwargs.get('price')\n    keys = f'{items}:{price}'\n    return keys\n\n\n\"\"\"GET\"\"\"\n\n\n@app.route('/example_1/\u003cstring:name\u003e', methods=['GET'])\n@ApiCache(expired_time=10)\ndef example_1(name):\n    \"\"\"\n    caching data in memory with default key.\n        - http://0.0.0.0:5000/example_1/jimmy\n    :param name:\n    :return:\n    \"\"\"\n    return jsonify(f'Hi {name}, it is {datetime.now()}')\n\n\n@app.route('/example_2/\u003cstring:name\u003e/\u003cint:age\u003e', methods=['GET'])\n@ApiCache(expired_time=10, key_func=custom_func_1)\ndef example_2(name, age):\n    \"\"\"\n    caching data in memory with custom function.\n        - http://0.0.0.0:5000/example_2/jimmy/18?sex=boy\n    :param name:\n    :param age:\n    :return:\n    \"\"\"\n    sex = request.args.get('sex', 'boy', str)\n    return jsonify(f'{name} is a {age} years old {sex}. {datetime.now()}')\n\n\n@app.route('/example_3/\u003cstring:items\u003e', methods=['GET'])\n@ApiCache(redis=redis_instance, expired_time=10)\ndef example_3(items):\n    \"\"\"\n    caching data in redis instance with default key.\n        - http://0.0.0.0:5000/example_3/coffee\n    :param items:\n    :return:\n    \"\"\"\n    return jsonify(f'You bought {items} at {datetime.now()}')\n\n\n@app.route('/example_4/\u003cstring:items\u003e/\u003cint:price\u003e', methods=['GET'])\n@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)\ndef example_4(items, price):\n    \"\"\"\n    caching data in redis instance with custom function.\n        - http://0.0.0.0:5000/example_4/coffee/20\n    :param items:\n    :param price:\n    :return:\n    \"\"\"\n    return jsonify(f'You bought {items} at {datetime.now()}, it cost ${price}')\n\n\n\"\"\"POST / PUT / DELETE\"\"\"\n\n\n@app.route('/example_5/\u003cstring:name\u003e', methods=['POST', 'PUT', 'DELETE'])\n@ApiCache(expired_time=10)\ndef example_5(name):\n    \"\"\"\n    caching data in memory with default key.\n        - http://0.0.0.0:5000/example_5/jimmy\n        - payload:\n            {\n                \"items\": \"coffee\",\n                \"price\": 18\n            }\n    :param name:\n    :return:\n    \"\"\"\n    payload = request.json\n    result = dict()\n    result['payload'] = payload\n    result['greeting'] = f'Hi {name}, it is {datetime.now()}'\n    return jsonify(result)\n\n\n@app.route('/example_6/\u003cstring:name\u003e', methods=['POST', 'PUT', 'DELETE'])\n@ApiCache(redis=redis_instance, expired_time=10)\ndef example_6(name):\n    \"\"\"\n    caching data in redis instance with default key.\n        - http://0.0.0.0:5000/example_6/jimmy\n        - payload:\n            {\n                \"items\": \"coffee\",\n                \"price\": 18\n            }\n    :param name:\n    :return:\n    \"\"\"\n    payload = request.json\n    result = dict()\n    result['payload'] = payload\n    result['greeting'] = f'Hi {name}, it is {datetime.now()}'\n    return result\n\n\n@app.route('/example_7/\u003cstring:name\u003e', methods=['POST', 'PUT', 'DELETE'])\n@ApiCache(redis=redis_instance, key_func=custom_func_2, expired_time=10)\ndef example_7(name):\n    \"\"\"\n    caching data in redis instance with custom function.\n        - http://0.0.0.0:5000/example_7/jimmy\n        - payload:\n            {\n                \"items\": \"coffee\",\n                \"price\": 18\n            }\n    :param name:\n    :return:\n    \"\"\"\n    payload = request.json\n    result = dict()\n    result['payload'] = payload\n    result['greeting'] = f'Hi {name}, it is {datetime.now()}'\n    return result\n\n\nif __name__ == '__main__':\n    app.run('0.0.0.0', port=5000, debug=True)\n\n\n```\n\n---\n\u003ca href=\"https://www.buymeacoffee.com/jimmyyyeh\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/default-blue.png\" alt=\"Buy Me A Coffee\" height=\"40\" width=\"175\"\u003e\u003c/a\u003e\n\n**Buy me a coffee, if you like it!**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmyyyeh%2Fflask-api-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjimmyyyeh%2Fflask-api-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjimmyyyeh%2Fflask-api-cache/lists"}