{"id":31049286,"url":"https://github.com/timbroddin/megacache","last_synced_at":"2025-09-14T21:50:18.529Z","repository":{"id":139337758,"uuid":"1537008","full_name":"TimBroddin/MegaCache","owner":"TimBroddin","description":"PHP cache abstraction layer","archived":false,"fork":false,"pushed_at":"2011-03-28T15:53:00.000Z","size":176,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-12T04:01:06.026Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TimBroddin.png","metadata":{"files":{"readme":"README","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-03-28T14:55:15.000Z","updated_at":"2014-01-29T03:30:03.000Z","dependencies_parsed_at":"2023-03-16T06:15:54.516Z","dependency_job_id":null,"html_url":"https://github.com/TimBroddin/MegaCache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/TimBroddin/MegaCache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimBroddin%2FMegaCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimBroddin%2FMegaCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimBroddin%2FMegaCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimBroddin%2FMegaCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimBroddin","download_url":"https://codeload.github.com/TimBroddin/MegaCache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimBroddin%2FMegaCache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275174438,"owners_count":25418064,"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-09-14T02:00:10.474Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":[],"created_at":"2025-09-14T21:50:15.781Z","updated_at":"2025-09-14T21:50:18.519Z","avatar_url":"https://github.com/TimBroddin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"                                           _           \n                                          | |          \n ____  _____  ____ _____  ____ _____  ____| |__  _____ \n|    \\| ___ |/ _  (____ |/ ___|____ |/ ___)  _ \\| ___ |\n| | | | ____( (_| / ___ ( (___/ ___ ( (___| | | | ____|\n|_|_|_|_____)\\___ \\_____|\\____)_____|\\____)_| |_|_____)\n            (_____|                                    \n\nversion 1.0\n\nLicensed as LGPL.\n\nThis document will assist you with setting up and exploring all the possibilities of this library.\n\n\n1) Introduction\n----------------\n\nMake your website super fast by using caching! Everyone hates it when they have to wait for more than three seconds to view \na webpage. Reasons for slowness are sudden traffic spikes, slow database servers, complex number crunching, ...\n\nThe easiest way to speed up any website is by caching the data that makes it slow. Enter MegaCache!\n\n2) Features\n-----------\n\n- Eight adapters. Store your cache where and how you want.\n- Function call caching. Cache the results of functions.\n- Resource caching. Cache external resources such as API calls (Twitter, ...) for a predefined amount of time.\n- Easy to use and implement.\n- Statistics. Check the hits and misses of each cache call.\n- Array access. Treat your cache object like an array!\n- Documented code!\n\n\n3) Adapters\n-----------\n\nMegaCache is bundled with 8 adapters (8 different ways to cache your data):\n\n- Null: \n\tthe most basic of caching strategies. Keeps things in memory and forgets them after the page is loaded. Use if you\n\tdon't have any other option.\n- Sqlite: \n\tuses the built in Sqlite support. Sqlite is an embedded database (you don't have to install anything) with some\n\tadvanced features (such as locking) that performs quite well. Very fast if you read a lot of data, slower\n\tif you write a lot of data.\n- SingleFile:\n\tstores everything in a single file. Very fast but not thread-safe (meaning that if two users make the same\n\trequest the last write will be remembered).\n- Session: \n\tstores data in the user's $_SESSION variable. Fast but not persistent across users.\n- APC: \n\tstores data in memory thanks to the APC (Alternative PHP Cache). The fastest across the board. Requires the APC\n\textension.\n- Memcache and Memcached:\n\ttwo adapters to connect to one or more Memcached servers. The first one requires the memcache extension, the \n\tsecond one required the memcached extension. Recommended for large installations.\n- PDO:\n    \tutilizes the Portable Data Objects extension which enables you to connect to a large number of different databases.\n    \tOnly tested with MySQL but should work with most vendors.\n\t\n4) Initialization\n-----------------\n\nThis library comes with a factory class that allows you to initialize any adapter. Usage is very simple:\n\n\u003c?php\nrequire('path/to/CacheFactory.php');\n$cache = CacheFactory::factory('Apc', $configArray='');\n?\u003e \n\nThe first argument is the adapter name, the second one is an array containing configuration options. Please consult the class documentation\nto view the configuration options for each adapter.\n\n5) Usage\n---------\n\n\t----------------\n\ta) storing data\n\t----------------\n\t\n\t$cache-\u003eset('myVar', $value, $timeout);\n\t\n\tStores $value in myVar for $timeout seconds. If you pass 0 then it will be cached forever (or until the cache gets full or cleared).\n\t\n\t-----------------\n\tb) fetching data\n\t-----------------\n\t\n\t$value = $cache-\u003eget('myVar');\n\t\n\treturns the data or false\n\t\n\t-----------------\n\tc) deleting data\n\t-----------------\n\t\n\t$cache-\u003edelete('myVar');\n\n\n6) Function caching\n--------------------\n\nIn many situations you'll want to cache the result of a function. Say for example we have a very complicated and time consuming function that\ntakes two numbers and makes the sum of both:\n\nfunction sum($a, $b) {\n\tsleep(5); // we have to simulate it's complicated right?\n}\n\nNormally you would do:\n\necho \"The sum of $a and $b is \" . sum($a, $b);\n\nAnd you would see the result 5 seconds later. With MegaCache you can simply do:\n\necho \"The sum of $a and $b is \" . $cache-\u003ecall('sum', array($a, $b), 120);\n\nThe first time will also take 5 seconds. However, afterwards the result will be cached for 120 seconds and your complex calculation will be executed.\nIf you change a or b, the function will run again.\n\n\n7) Fragment caching\n--------------------\n\nOften only parts of your website need to be cached. This can be accomplished with fragment caching.\n\nTake for example this code:\n\n$newsticker = new NewsTicker(); // an imaginary widget that does a lot of processing\nforeach($newsticker-\u003enews as $item) {\n\techo \"\u003ch2\u003eItem\u003c/h2\u003e\";\n}\n\nIf you want this fragment of your page to be cached simply wrap it in a fragment conditional.\n\nif(!$cache-\u003efragment('newsticker')) {\n\t$newsticker = new NewsTicker(); // an imaginary widget that does a lot of processing\n\tforeach($newsticker-\u003enews as $item) {\n\t\techo \"\u003ch2\u003eItem\u003c/h2\u003e\";\n\t}\t\n\t$this-\u003esaveFragment(300);\n}\n\nThis will cache the newsticker component for 5 minutes. Each next page view the fragment call will automatically output the newsticker.\n\n\n8) Resource caching\n--------------------\n\nPretend you want to show a Twitter feed on your webpage. Normally every page load will fetch your Twitter RSS feed. Since Twitter can be slow,\nyour website may also feel slow sometimes.\n\nNormally you would do:\n\n$twitter = file_get_contents('http://twitter.com/statuses/user_timeline/61297416.rss');\n// and then mark up this code\n\nWith MegaCache you can simply do:\n$twitter = $cache-\u003efetch('http://twitter.com/statuses/user_timeline/61297416.rss', 300);\n\nAnd your twitter feed will be cached for 5 minutes!\n\n\n9) Statistics\n--------------\n\nIf you call $cache-\u003egetStats(); you will receive an array containing all the hits, misses, sets, deletes, gets, ... of both the current page view (session) \nand the sum of all requests.\n\n\n10) Array access\n---------------\n\nAt any time you can use your cache object like an array. Sets made will have timeout 0.\n\n$cache-\u003eset('myVar', 10);\necho $cache['myVar']; //10\n\n11) Benchmarking\n-----------------\n\nThe test directory contains a small test scenario that measures the speed of the various adapters. This is not a real world test (at all) \nbut it gives a good indication of the speed of the various adapters.\n\nThese are my results.\n\t- Null: disqualified since it is not persistent.\n\t- Sqlite: 1,5x faster than without caching. Main reason is it seems to be very slow on inserts.\n\t- SingleFile: 45x faster than without caching.\n\t- Session: 6x faster.\n\t- APC: 51x faster. Use this!\n\t- Memcache: 16x faster.\n\t- Memcached: 13x faster. Weird enough slower than Memcache across the board (library overhead?).\n\t- PDO: 5x faster. Also slower because of DB inserts.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimbroddin%2Fmegacache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimbroddin%2Fmegacache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimbroddin%2Fmegacache/lists"}