<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: memcache code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 21:59:32 GMT</pubDate>
    <description>DZone Snippets: memcache code</description>
    <item>
      <title>Fake memcache server</title>
      <link>http://snippets.dzone.com/posts/show/6124</link>
      <description>A small fake memcache, mimics the 'import memcache' module Useful for testing without a real memcache server to connect to, nowhere near complete (missing methods like flush_all()) for example, although if someone was to take the time...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class Memcache(object):&lt;br /&gt;    """&lt;br /&gt;    usage:&lt;br /&gt;    &gt;&gt;&gt; mc = Memcache()&lt;br /&gt;    &gt;&gt;&gt; mc.set('joe', 4)&lt;br /&gt;    &gt;&gt;&gt; mc.get('joe')&lt;br /&gt;    4&lt;br /&gt;    """&lt;br /&gt;&lt;br /&gt;    cache = {}&lt;br /&gt;&lt;br /&gt;    def set(self, key, value):&lt;br /&gt;        assert(isinstance(key, basestring))&lt;br /&gt;        self.cache[key] = value&lt;br /&gt;&lt;br /&gt;    def set_multi(self, keys_values):&lt;br /&gt;        for key in keys_values:&lt;br /&gt;            assert(isinstance(key, basestring))&lt;br /&gt;        self.cache.update(keys_values)&lt;br /&gt;&lt;br /&gt;    def get(self, key):&lt;br /&gt;        assert(isinstance(key, basestring))&lt;br /&gt;        return self.cache.get(key, None)&lt;br /&gt;&lt;br /&gt;    def get_multi(self, key_list, key_prefix=''):&lt;br /&gt;        for key in key_list:&lt;br /&gt;            assert(isinstance(key, basestring))&lt;br /&gt;        result = {}&lt;br /&gt;        for key in key_list:&lt;br /&gt;            ckey = key_prefix+key&lt;br /&gt;            if ckey in self.cache:&lt;br /&gt;                result[key] = self.cache[ckey]&lt;br /&gt;        return result&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 22 Sep 2008 15:21:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/6124</guid>
      <author>zepolen (zepolen)</author>
    </item>
    <item>
      <title>Time based expiration of cache fragments in Ruby on Rails (MemCacheStore and FileStore)</title>
      <link>http://snippets.dzone.com/posts/show/2975</link>
      <description>This allows to expire cached fragments by ttl. Example usage (in views):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% cache("name", :ttl =&gt; 7.days) do %&gt;&lt;br /&gt;  ... some database-heavy stuff ...&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Put the following in environment.rb or in lib/.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ActionController::Caching::Fragments::MemCacheStore&lt;br /&gt;  def write(name, value, options=nil)&lt;br /&gt;    if options.is_a?(Hash) &amp;&amp; options.has_key?(:ttl)&lt;br /&gt;      ttl = options[:ttl]&lt;br /&gt;    else&lt;br /&gt;      ttl = 0&lt;br /&gt;    end&lt;br /&gt;    @data.set(name, value, ttl)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;module ActionView::Helpers::CacheHelper&lt;br /&gt;  def cache(name = {}, options = nil, &amp;block)&lt;br /&gt;    @controller.cache_erb_fragment(block, name, options)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class ActionController::Caching::Fragments::UnthreadedFileStore&lt;br /&gt;  def read(name, options = nil) &lt;br /&gt;    if options.is_a?(Hash) &amp;&amp; options.has_key?(:ttl)&lt;br /&gt;      ttl = options[:ttl]&lt;br /&gt;    else&lt;br /&gt;      ttl = 0&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    fn = real_file_path(name)&lt;br /&gt;&lt;br /&gt;    # if cache expired act as if file doesn't exist&lt;br /&gt;    return if ttl &gt; 0 &amp;&amp; File.exists?(fn) &amp;&amp; (File.mtime(fn) &lt; (Time.now - ttl))&lt;br /&gt;&lt;br /&gt;    File.open(fn, 'rb') { |f| f.read } rescue nil&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 06 Nov 2006 13:00:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2975</guid>
      <author>jacek.becela (Jacek Becela)</author>
    </item>
  </channel>
</rss>
