{"id":2952,"date":"2012-08-15T23:41:43","date_gmt":"2012-08-16T04:41:43","guid":{"rendered":"http:\/\/icesquare.com\/wordpress\/?p=2952"},"modified":"2014-07-06T18:43:03","modified_gmt":"2014-07-06T23:43:03","slug":"delete-memcache-key-in-php","status":"publish","type":"post","link":"https:\/\/icesquare.com\/wordpress\/delete-memcache-key-in-php\/","title":{"rendered":"[PHP]How to delete Memcache variables in PHP?"},"content":{"rendered":"<p>I&#8217;ve never thought about deleting a Memcache key is a difficult task. It took me few hours to figure out a working solution. No kidding. FYI, I am using PHP 5.4<\/p>\n<p>Suppose you store a very expensive data into the Memcache, and you decide to delete it later. According to the PHP documentation, it should be very simple:<\/p>\n<pre style='margin-top:50px;'>\r\n$memcache_obj->delete('key_to_delete');\r\n\r\nor\r\n\r\nmemcache_delete($memcache_obj, 'key_to_delete');\r\n<\/pre>\n<p>\nHowever, this method only works with scalar variables, such as string, integer, double, float, Boolean etc. It does not work with non-scalar variables such as array, object etc.\n<\/p>\n<p>\nI have tried many different ways, such as setting the value to something else, e.g.,\n<\/p>\n<pre>\r\n$memcache->set('key_to_delete', 'deleted');\r\n<\/pre>\n<p>or making the key to expire immediately:<\/p>\n<pre>\r\n$memcache->set('key_to_delete', 'deleted', 0, 1);\r\n$memcache->set('key_to_delete', 'deleted', 0, -1);\r\n<\/pre>\n<p>Unfortunately, none of these methods worked in my case (my variable is a non-scalar array). So, I ended up doing it in a low-level way: Bypassing the PECL-Memcache (PHP Library) and talk to the Memcache server directly. Guess what, it works very well. The variable gets deleted!<\/p>\n<pre>\r\nfunction MEMCACHE_DEL_HASH($key){\r\n\t\r\n\tif ($key == '') return FALSE;\r\n\t\r\n\t$socket = @fsockopen('localhost', 11211);\r\n\t$command = \"delete {$key}\";\r\n\t\r\n\tfwrite($socket, $command.\"\\r\\n\");\r\n\tfclose($socket);\r\n\t\r\n\treturn true;\r\n}\r\n<\/pre>\n<p>And it is super easy to call this function:<\/p>\n<pre>\r\nMEMCACHE_DEL_HASH('key_to_delete');\r\n<\/pre>\n<p>Feel free to modify this function such that you can pass in the connector, port or do anything you like.<\/p>\n<p>That&#8217;s it! Have fun with Memcache.<\/p>\n<p>&#8211;Derrick<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve never thought about deleting a Memcache key is a difficult task. It took me few hours to figure out a working solution. No kidding. FYI, I am using PHP <a class=\"more-link\" href=\"https:\/\/icesquare.com\/wordpress\/delete-memcache-key-in-php\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":2953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[94,10,6,69],"tags":[11,172],"class_list":["post-2952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-problems-computer-help-pc-support","category-php","category-technology","category-troubleshooting-technology","tag-memcached","tag-php"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p1sqzr-LC","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":31,"url":"https:\/\/icesquare.com\/wordpress\/installing-apache-php-mysql-and-memcached-on-freebsd\/","url_meta":{"origin":2952,"position":0},"title":"Installing Apache, PHP, MySQL and MemCached on FreeBSD","author":"Derrick","date":"May 19, 2009","format":false,"excerpt":"This is my note on how to to install Apache, PHP, MySQL and MemCached on a FreeBSD 7 Box. 1. MySQL Go to the mysql52-server port directory by typing the command: cd \/usr\/ports\/databases\/mysql51-server Build the port by typing: make BUILD_OPTIMIZED=yes BUILD_STATIC=yes Using these two options will speed up the execution\u2026","rel":"","context":"In &quot;FreeBSD&quot;","block_context":{"text":"FreeBSD","link":"https:\/\/icesquare.com\/wordpress\/category\/sever-problem\/freebsd\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":41,"url":"https:\/\/icesquare.com\/wordpress\/installing-apache-php-mysql-and-memcached-on-os-x\/","url_meta":{"origin":2952,"position":1},"title":"Installing Apache, PHP, MySQL and MemCached on OS X","author":"Derrick","date":"May 19, 2009","format":false,"excerpt":"This is my note on how to to install Apache, PHP and MySQL on a an OS X box. Although some of these applications are come with the operating systems, it is always good to know how to upgrade them to the latest version. 1. Apple Developer Tools and Mac\u2026","rel":"","context":"In &quot;Server Problems&quot;","block_context":{"text":"Server Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/sever-problem\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2502,"url":"https:\/\/icesquare.com\/wordpress\/freebsdproblem-to-update-php-port\/","url_meta":{"origin":2952,"position":2},"title":"[FreeBSD]Problem to update PHP port","author":"Derrick","date":"January 12, 2012","format":false,"excerpt":"A new version of PHP (5.3.9) was available in FreeBSD today. Since it contained a lot of security fixes and enhancements, I decided to give it a try. After testing the new PHP in couple test servers, I think it is ready to upgrade the PHP on the production server.\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1927,"url":"https:\/\/icesquare.com\/wordpress\/phphow-to-get-the-number-of-cpu-cores-in-fedora-ubuntu-and-freebsd\/","url_meta":{"origin":2952,"position":3},"title":"[PHP]How to get the number of CPU cores in Fedora, Ubuntu, and FreeBSD","author":"Derrick","date":"December 2, 2011","format":false,"excerpt":"I am working on a PHP application which needs to be deploy to multiple places. One of the variables is the number of CPU core. Since each server has different number of CPU cores, I need to specify the number of CPU core in my application for each server. I\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3514,"url":"https:\/\/icesquare.com\/wordpress\/freebsdphp-does-not-work-after-upgrading-apache-to-2-2-27\/","url_meta":{"origin":2952,"position":4},"title":"[FreeBSD]PHP does not work after upgrading Apache to 2.2.27","author":"Derrick","date":"April 11, 2014","format":false,"excerpt":"I upgraded the Apache to 2.2.27 on my FreeBSD box via portmaster. The upgrade went very smooth. After the upgrade, I found that Apache no longer rendered the PHP page correctly. In the other words, it displayed the source code of the PHP files instead of executing the code. Before\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1489,"url":"https:\/\/icesquare.com\/wordpress\/php-warning-php-startup-unable-to-load-dynamic-library-usrlocallibphp20090626tokyo_tyrant-so-usrlocallibphp20090626tokyo_tyrant-so-undefined-symbol-ps_globals-in-unknown-on-l\/","url_meta":{"origin":2952,"position":5},"title":"PHP Warning:  PHP Startup: Unable to load dynamic library &#8216;\/usr\/local\/lib\/php\/20090626\/tokyo_tyrant.so&#8217; &#8211; \/usr\/local\/lib\/php\/20090626\/tokyo_tyrant.so: Undefined symbol &#8220;ps_globals&#8221; in Unknown on line 0","author":"Derrick","date":"August 23, 2011","format":false,"excerpt":"Every time I upgrade the PHP and related extensions, I always run into the same problem with some PHP-PECL modules. Recently with the new release of PHP 5.3.7, there is no exception for the trouble. So what kind of trouble I have today? The Tokyo Tyrant (The middleman between my\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"\/wordpress\/wp-content\/uploads\/2011\/08\/tokyocabinet.png","width":350,"height":200},"classes":[]}],"jetpack_featured_media_url":"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2012\/08\/how-to-delete-files-on-macbook.gif","_links":{"self":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/2952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2952"}],"version-history":[{"count":17,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/2952\/revisions"}],"predecessor-version":[{"id":3666,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/2952\/revisions\/3666"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/media\/2953"}],"wp:attachment":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}