ShogunPanda/elephas

View on GitHub
doc/Elephas/Entry.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
  Class: Elephas::Entry
  
    &mdash; Documentation by YARD 0.8.6.2
  
</title>

  <link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />

  <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  hasFrames = window.top.frames.main ? true : false;
  relpath = '../';
  framesUrl = "../frames.html#!" + escape(window.location.href);
</script>


  <script type="text/javascript" charset="utf-8" src="../js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="../js/app.js"></script>


  </head>
  <body>
    <div id="header">
      <div id="menu">
  
    <a href="../_index.html">Index (E)</a> &raquo;
    <span class='title'><span class='object_link'><a href="../Elephas.html" title="Elephas (module)">Elephas</a></span></span>
     &raquo; 
    <span class="title">Entry</span>
  

  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="../class_list.html">
      Class List
    </a>
  
    <a class="full_list_link" id="method_list_link"
        href="../method_list.html">
      Method List
    </a>
  
    <a class="full_list_link" id="file_list_link"
        href="../file_list.html">
      File List
    </a>
  
</div>
      <div class="clear"></div>
    </div>

    <iframe id="search_frame"></iframe>

    <div id="content"><h1>Class: Elephas::Entry
  
  
  
</h1>

<dl class="box">
  
    <dt class="r1">Inherits:</dt>
    <dd class="r1">
      <span class="inheritName">Object</span>
      
        <ul class="fullTree">
          <li>Object</li>
          
            <li class="next">Elephas::Entry</li>
          
        </ul>
        <a href="#" class="inheritanceTree">show all</a>
      
      </dd>
    
  
  
    
  
    
  
  
  
    <dt class="r2 last">Defined in:</dt>
    <dd class="r2 last">lib/elephas/entry.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    <p>Represents a cache entry.</p>


  </div>
</div>
<div class="tags">
  

</div>



  <h2>Instance Attribute Summary <small>(<a href="#" class="summary_toggle">collapse</a>)</small></h2>
  <ul class="summary">
    
      <li class="public ">
  <span class="summary_signature">
    
      <a href="#hash-instance_method" title="#hash (instance method)">- (String) <strong>hash</strong> </a>
    

    
  </span>
  
  
  
    
    
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>The hashed (unique) key for this entry.</p>
</div></span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature">
    
      <a href="#key-instance_method" title="#key (instance method)">- (String) <strong>key</strong> </a>
    

    
  </span>
  
  
  
    
    
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>The key for this entry.</p>
</div></span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature">
    
      <a href="#ttl-instance_method" title="#ttl (instance method)">- (Fixnum) <strong>ttl</strong> </a>
    

    
  </span>
  
  
  
    
    
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>The expected TTL of the entry, in milliseconds.</p>
</div></span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature">
    
      <a href="#updated_at-instance_method" title="#updated_at (instance method)">- (Fixnum) <strong>updated_at</strong> </a>
    

    
  </span>
  
  
  
    
    
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>The last update date of the entry, in UNIX timestamp (with milliseconds).</p>
</div></span>
  
</li>

    
      <li class="public ">
  <span class="summary_signature">
    
      <a href="#value-instance_method" title="#value (instance method)">- (Object) <strong>value</strong> </a>
    

    
  </span>
  
  
  
    
    
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>The value contained in this entry.</p>
</div></span>
  
</li>

    
  </ul>




  
    <h2>
      Class Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#ensure-class_method" title="ensure (class method)">+ (Entry) <strong>ensure</strong>(value, key, options = {}) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Ensures that the value is an Entry.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#hashify_key-class_method" title="hashify_key (class method)">+ (String) <strong>hashify_key</strong>(key) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Returns a unique hash for the key.</p>
</div></span>
  
</li>

      
    </ul>
  
    <h2>
      Instance Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#%3D%3D-instance_method" title="#== (instance method)">- (Boolean) <strong>==</strong>(other) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Compares to another Entry.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#initialize-instance_method" title="#initialize (instance method)">- (Entry) <strong>initialize</strong>(key, value, hash = nil, ttl = 360000) </a>
    

    
  </span>
  
  
    <span class="note title constructor">constructor</span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Creates a new entry.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#refresh-instance_method" title="#refresh (instance method)">- (Float) <strong>refresh</strong>(save = false, cache = nil) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Refreshes the entry.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#valid%3F-instance_method" title="#valid? (instance method)">- (Boolean) <strong>valid?</strong>(backend) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'><p>Checks if the entry is still valid.</p>
</div></span>
  
</li>

      
    </ul>
  

<div id="constructor_details" class="method_details_list">
  <h2>Constructor Details</h2>
  
    <div class="method_details first">
  <h3 class="signature first" id="initialize-instance_method">
  
    - (<tt><span class='object_link'><a href="" title="Elephas::Entry (class)">Entry</a></span></tt>) <strong>initialize</strong>(key, value, hash = nil, ttl = 360000) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Creates a new entry.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>key</span>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The key for this entry.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>value</span>
      
      
        <span class='type'>(<tt>Object</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The value contained in this entry.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>hash</span>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>nil</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>The hash for this entry. Should be unique. It is automatically created if not provided.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>ttl</span>
      
      
        <span class='type'>(<tt>Integer</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>360000</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>The time to live (TTL) for this entry. If set to 0 then the entry is not cached.</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


33
34
35
36
37
38
39</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 33</span>

<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
  <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
  <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
  <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
  <span class='id identifier rubyid_refresh'>refresh</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
  
</div>

  <div id="instance_attr_details" class="attr_details">
    <h2>Instance Attribute Details</h2>
    
      
      <span id="hash=-instance_method"></span>
      <div class="method_details first">
  <h3 class="signature first" id="hash-instance_method">
  
    - (<tt>String</tt>) <strong>hash</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>The hashed (unique) key for this entry.</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The hashed (unique) key for this entry.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 20</span>

<span class='kw'>class</span> <span class='const'>Entry</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:key</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:hash</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:value</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:ttl</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:updated_at</span>

  <span class='comment'># Creates a new entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key for this entry.
</span>  <span class='comment'># @param value [Object] The value contained in this entry.
</span>  <span class='comment'># @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
</span>  <span class='comment'># @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
    <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
    <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
    <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
    <span class='id identifier rubyid_refresh'>refresh</span>
  <span class='kw'>end</span>

  <span class='comment'># Refreshes the entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param save [Boolean] If to save the refresh value in the cache.
</span>  <span class='comment'># @param cache [Cache] The cache where to save the entry.
</span>  <span class='comment'># @return [Float] The new updated_at value.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
    <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
    <span class='ivar'>@updated_at</span>
  <span class='kw'>end</span>

  <span class='comment'># Checks if the entry is still valid.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param backend [Backends::Base] The backend to use for the check.
</span>  <span class='comment'># @return [Boolean] `true` if the entry is still valid, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
  <span class='kw'>end</span>

  <span class='comment'># Compares to another Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param other [Entry] The entry to compare with
</span>  <span class='comment'># @return [Boolean] `true` if the entries are the same, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>

  <span class='comment'># Returns a unique hash for the key.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key to hashify.
</span>  <span class='comment'># @return [String] An unique hash for the key.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='comment'># Ensures that the value is an Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param value [Object] The object to check.
</span>  <span class='comment'># @param key [Object] The key associated to this object.
</span>  <span class='comment'># @param options [Hash] Options to manage the value.
</span>  <span class='comment'># @return [Entry] The wrapped object.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

    <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
      <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

      <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
      <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
    <span class='kw'>end</span>

    <span class='id identifier rubyid_rv'>rv</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>
    <span class='comment'># Makes sure a new updated at is generated.
</span>    <span class='comment'>#
</span>    <span class='comment'># @param initial [Float] Old value.
</span>    <span class='comment'># @return [Float] New value.
</span>    <span class='kw'>def</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='id identifier rubyid_initial'>initial</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>+=</span> <span class='float'>1E6</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>==</span> <span class='id identifier rubyid_initial'>initial</span>
      <span class='id identifier rubyid_rv'>rv</span>
    <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      
      <span id="key=-instance_method"></span>
      <div class="method_details ">
  <h3 class="signature " id="key-instance_method">
  
    - (<tt>String</tt>) <strong>key</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>The key for this entry.</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The key for this entry.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 20</span>

<span class='kw'>class</span> <span class='const'>Entry</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:key</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:hash</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:value</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:ttl</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:updated_at</span>

  <span class='comment'># Creates a new entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key for this entry.
</span>  <span class='comment'># @param value [Object] The value contained in this entry.
</span>  <span class='comment'># @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
</span>  <span class='comment'># @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
    <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
    <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
    <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
    <span class='id identifier rubyid_refresh'>refresh</span>
  <span class='kw'>end</span>

  <span class='comment'># Refreshes the entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param save [Boolean] If to save the refresh value in the cache.
</span>  <span class='comment'># @param cache [Cache] The cache where to save the entry.
</span>  <span class='comment'># @return [Float] The new updated_at value.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
    <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
    <span class='ivar'>@updated_at</span>
  <span class='kw'>end</span>

  <span class='comment'># Checks if the entry is still valid.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param backend [Backends::Base] The backend to use for the check.
</span>  <span class='comment'># @return [Boolean] `true` if the entry is still valid, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
  <span class='kw'>end</span>

  <span class='comment'># Compares to another Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param other [Entry] The entry to compare with
</span>  <span class='comment'># @return [Boolean] `true` if the entries are the same, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>

  <span class='comment'># Returns a unique hash for the key.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key to hashify.
</span>  <span class='comment'># @return [String] An unique hash for the key.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='comment'># Ensures that the value is an Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param value [Object] The object to check.
</span>  <span class='comment'># @param key [Object] The key associated to this object.
</span>  <span class='comment'># @param options [Hash] Options to manage the value.
</span>  <span class='comment'># @return [Entry] The wrapped object.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

    <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
      <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

      <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
      <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
    <span class='kw'>end</span>

    <span class='id identifier rubyid_rv'>rv</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>
    <span class='comment'># Makes sure a new updated at is generated.
</span>    <span class='comment'>#
</span>    <span class='comment'># @param initial [Float] Old value.
</span>    <span class='comment'># @return [Float] New value.
</span>    <span class='kw'>def</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='id identifier rubyid_initial'>initial</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>+=</span> <span class='float'>1E6</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>==</span> <span class='id identifier rubyid_initial'>initial</span>
      <span class='id identifier rubyid_rv'>rv</span>
    <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      
      <span id="ttl=-instance_method"></span>
      <div class="method_details ">
  <h3 class="signature " id="ttl-instance_method">
  
    - (<tt>Fixnum</tt>) <strong>ttl</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>The expected TTL of the entry, in milliseconds.</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Fixnum</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The expected TTL of the entry, in milliseconds.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 20</span>

<span class='kw'>class</span> <span class='const'>Entry</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:key</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:hash</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:value</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:ttl</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:updated_at</span>

  <span class='comment'># Creates a new entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key for this entry.
</span>  <span class='comment'># @param value [Object] The value contained in this entry.
</span>  <span class='comment'># @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
</span>  <span class='comment'># @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
    <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
    <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
    <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
    <span class='id identifier rubyid_refresh'>refresh</span>
  <span class='kw'>end</span>

  <span class='comment'># Refreshes the entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param save [Boolean] If to save the refresh value in the cache.
</span>  <span class='comment'># @param cache [Cache] The cache where to save the entry.
</span>  <span class='comment'># @return [Float] The new updated_at value.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
    <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
    <span class='ivar'>@updated_at</span>
  <span class='kw'>end</span>

  <span class='comment'># Checks if the entry is still valid.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param backend [Backends::Base] The backend to use for the check.
</span>  <span class='comment'># @return [Boolean] `true` if the entry is still valid, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
  <span class='kw'>end</span>

  <span class='comment'># Compares to another Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param other [Entry] The entry to compare with
</span>  <span class='comment'># @return [Boolean] `true` if the entries are the same, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>

  <span class='comment'># Returns a unique hash for the key.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key to hashify.
</span>  <span class='comment'># @return [String] An unique hash for the key.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='comment'># Ensures that the value is an Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param value [Object] The object to check.
</span>  <span class='comment'># @param key [Object] The key associated to this object.
</span>  <span class='comment'># @param options [Hash] Options to manage the value.
</span>  <span class='comment'># @return [Entry] The wrapped object.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

    <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
      <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

      <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
      <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
    <span class='kw'>end</span>

    <span class='id identifier rubyid_rv'>rv</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>
    <span class='comment'># Makes sure a new updated at is generated.
</span>    <span class='comment'>#
</span>    <span class='comment'># @param initial [Float] Old value.
</span>    <span class='comment'># @return [Float] New value.
</span>    <span class='kw'>def</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='id identifier rubyid_initial'>initial</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>+=</span> <span class='float'>1E6</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>==</span> <span class='id identifier rubyid_initial'>initial</span>
      <span class='id identifier rubyid_rv'>rv</span>
    <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      
      <span id="updated_at=-instance_method"></span>
      <div class="method_details ">
  <h3 class="signature " id="updated_at-instance_method">
  
    - (<tt>Fixnum</tt>) <strong>updated_at</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>The last update date of the entry, in UNIX timestamp (with milliseconds).</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Fixnum</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The last update date of the entry, in UNIX timestamp (with milliseconds).</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 20</span>

<span class='kw'>class</span> <span class='const'>Entry</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:key</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:hash</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:value</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:ttl</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:updated_at</span>

  <span class='comment'># Creates a new entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key for this entry.
</span>  <span class='comment'># @param value [Object] The value contained in this entry.
</span>  <span class='comment'># @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
</span>  <span class='comment'># @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
    <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
    <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
    <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
    <span class='id identifier rubyid_refresh'>refresh</span>
  <span class='kw'>end</span>

  <span class='comment'># Refreshes the entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param save [Boolean] If to save the refresh value in the cache.
</span>  <span class='comment'># @param cache [Cache] The cache where to save the entry.
</span>  <span class='comment'># @return [Float] The new updated_at value.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
    <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
    <span class='ivar'>@updated_at</span>
  <span class='kw'>end</span>

  <span class='comment'># Checks if the entry is still valid.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param backend [Backends::Base] The backend to use for the check.
</span>  <span class='comment'># @return [Boolean] `true` if the entry is still valid, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
  <span class='kw'>end</span>

  <span class='comment'># Compares to another Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param other [Entry] The entry to compare with
</span>  <span class='comment'># @return [Boolean] `true` if the entries are the same, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>

  <span class='comment'># Returns a unique hash for the key.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key to hashify.
</span>  <span class='comment'># @return [String] An unique hash for the key.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='comment'># Ensures that the value is an Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param value [Object] The object to check.
</span>  <span class='comment'># @param key [Object] The key associated to this object.
</span>  <span class='comment'># @param options [Hash] Options to manage the value.
</span>  <span class='comment'># @return [Entry] The wrapped object.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

    <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
      <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

      <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
      <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
    <span class='kw'>end</span>

    <span class='id identifier rubyid_rv'>rv</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>
    <span class='comment'># Makes sure a new updated at is generated.
</span>    <span class='comment'>#
</span>    <span class='comment'># @param initial [Float] Old value.
</span>    <span class='comment'># @return [Float] New value.
</span>    <span class='kw'>def</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='id identifier rubyid_initial'>initial</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>+=</span> <span class='float'>1E6</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>==</span> <span class='id identifier rubyid_initial'>initial</span>
      <span class='id identifier rubyid_rv'>rv</span>
    <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      
      <span id="value=-instance_method"></span>
      <div class="method_details ">
  <h3 class="signature " id="value-instance_method">
  
    - (<tt>Object</tt>) <strong>value</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>The value contained in this entry.</p>


  </div>
</div>
<div class="tags">
  
<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Object</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The value contained in this entry.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 20</span>

<span class='kw'>class</span> <span class='const'>Entry</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:key</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:hash</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:value</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:ttl</span>
  <span class='id identifier rubyid_attr_accessor'>attr_accessor</span> <span class='symbol'>:updated_at</span>

  <span class='comment'># Creates a new entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key for this entry.
</span>  <span class='comment'># @param value [Object] The value contained in this entry.
</span>  <span class='comment'># @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
</span>  <span class='comment'># @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='int'>360000</span><span class='rparen'>)</span>
    <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
    <span class='ivar'>@hash</span> <span class='op'>=</span> <span class='id identifier rubyid_hash'>hash</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span> <span class='op'>?</span> <span class='id identifier rubyid_hash'>hash</span> <span class='op'>:</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='ivar'>@value</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>
    <span class='ivar'>@ttl</span> <span class='op'>=</span> <span class='id identifier rubyid_ttl'>ttl</span>
    <span class='id identifier rubyid_refresh'>refresh</span>
  <span class='kw'>end</span>

  <span class='comment'># Refreshes the entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param save [Boolean] If to save the refresh value in the cache.
</span>  <span class='comment'># @param cache [Cache] The cache where to save the entry.
</span>  <span class='comment'># @return [Float] The new updated_at value.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
    <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
    <span class='ivar'>@updated_at</span>
  <span class='kw'>end</span>

  <span class='comment'># Checks if the entry is still valid.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param backend [Backends::Base] The backend to use for the check.
</span>  <span class='comment'># @return [Boolean] `true` if the entry is still valid, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
  <span class='kw'>end</span>

  <span class='comment'># Compares to another Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param other [Entry] The entry to compare with
</span>  <span class='comment'># @return [Boolean] `true` if the entries are the same, `false` otherwise.
</span>  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
  <span class='kw'>end</span>

  <span class='comment'># Returns a unique hash for the key.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param key [String] The key to hashify.
</span>  <span class='comment'># @return [String] An unique hash for the key.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
    <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='comment'># Ensures that the value is an Entry.
</span>  <span class='comment'>#
</span>  <span class='comment'># @param value [Object] The object to check.
</span>  <span class='comment'># @param key [Object] The key associated to this object.
</span>  <span class='comment'># @param options [Hash] Options to manage the value.
</span>  <span class='comment'># @return [Entry] The wrapped object.
</span>  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

    <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
      <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

      <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
      <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
    <span class='kw'>end</span>

    <span class='id identifier rubyid_rv'>rv</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_private'>private</span>
    <span class='comment'># Makes sure a new updated at is generated.
</span>    <span class='comment'>#
</span>    <span class='comment'># @param initial [Float] Old value.
</span>    <span class='comment'># @return [Float] New value.
</span>    <span class='kw'>def</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='id identifier rubyid_initial'>initial</span><span class='rparen'>)</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='const'>Time</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span><span class='period'>.</span><span class='id identifier rubyid_to_f'>to_f</span>
      <span class='id identifier rubyid_rv'>rv</span> <span class='op'>+=</span> <span class='float'>1E6</span> <span class='kw'>if</span> <span class='id identifier rubyid_rv'>rv</span> <span class='op'>==</span> <span class='id identifier rubyid_initial'>initial</span>
      <span class='id identifier rubyid_rv'>rv</span>
    <span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>


  <div id="class_method_details" class="method_details_list">
    <h2>Class Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="ensure-class_method">
  
    + (<tt><span class='object_link'><a href="" title="Elephas::Entry (class)">Entry</a></span></tt>) <strong>ensure</strong>(value, key, options = {}) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Ensures that the value is an Entry.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>value</span>
      
      
        <span class='type'>(<tt>Object</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The object to check.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>key</span>
      
      
        <span class='type'>(<tt>Object</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The key associated to this object.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>options</span>
      
      
        <span class='type'>(<tt>Hash</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>{}</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>Options to manage the value.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Elephas::Entry (class)">Entry</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The wrapped object.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


82
83
84
85
86
87
88
89
90
91
92
93
94
95</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 82</span>

<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='kw'>ensure</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span>

  <span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_rv'>rv</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='kw'>then</span>
    <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_ensure_hash'>ensure_hash</span>

    <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:ttl</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_integer'>to_integer</span><span class='comma'>,</span> <span class='int'>0</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_max'>max</span>
    <span class='id identifier rubyid_hash'>hash</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:hash</span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>

    <span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_rv'>rv</span><span class='comma'>,</span> <span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_ttl'>ttl</span><span class='rparen'>)</span>
  <span class='kw'>end</span>

  <span class='id identifier rubyid_rv'>rv</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="hashify_key-class_method">
  
    + (<tt>String</tt>) <strong>hashify_key</strong>(key) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Returns a unique hash for the key.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>key</span>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The key to hashify.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>String</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>An unique hash for the key.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


72
73
74</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 72</span>

<span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_hashify_key'>hashify_key</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
  <span class='const'>Digest</span><span class='op'>::</span><span class='const'>SHA2</span><span class='period'>.</span><span class='id identifier rubyid_hexdigest'>hexdigest</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="==-instance_method">
  
    - (<tt>Boolean</tt>) <strong>==</strong>(other) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Compares to another Entry.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>other</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="" title="Elephas::Entry (class)">Entry</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The entry to compare with</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Boolean</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p><code>true</code> if the entries are the same, <code>false</code> otherwise.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


64
65
66</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 64</span>

<span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='op'>::</span><span class='const'>Elephas</span><span class='op'>::</span><span class='const'>Entry</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='lbracket'>[</span><span class='ivar'>@key</span><span class='comma'>,</span> <span class='ivar'>@hash</span><span class='comma'>,</span> <span class='ivar'>@value</span><span class='rbracket'>]</span> <span class='op'>==</span> <span class='lbracket'>[</span><span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_hash'>hash</span><span class='comma'>,</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_value'>value</span><span class='rbracket'>]</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="refresh-instance_method">
  
    - (<tt>Float</tt>) <strong>refresh</strong>(save = false, cache = nil) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Refreshes the entry.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>save</span>
      
      
        <span class='type'>(<tt>Boolean</tt>)</span>
      
      
        <em class="default">(defaults to: <tt>false</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>If to save the refresh value in the cache.</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>cache</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="Cache.html" title="Elephas::Cache (class)">Cache</a></span></tt>)</span>
      
      
        <em class="default">(defaults to: <tt>nil</tt>)</em>
      
      
        &mdash;
        <div class='inline'><p>The cache where to save the entry.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Float</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The new updated_at value.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


46
47
48
49
50</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 46</span>

<span class='kw'>def</span> <span class='id identifier rubyid_refresh'>refresh</span><span class='lparen'>(</span><span class='id identifier rubyid_save'>save</span> <span class='op'>=</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='id identifier rubyid_cache'>cache</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
  <span class='ivar'>@updated_at</span> <span class='op'>=</span> <span class='id identifier rubyid_get_new_updated_at'>get_new_updated_at</span><span class='lparen'>(</span><span class='ivar'>@updated_at</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_cache'>cache</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='ivar'>@hash</span><span class='comma'>,</span> <span class='kw'>self</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_save'>save</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_cache'>cache</span>
  <span class='ivar'>@updated_at</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="valid?-instance_method">
  
    - (<tt>Boolean</tt>) <strong>valid?</strong>(backend) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    <p>Checks if the entry is still valid.</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>backend</span>
      
      
        <span class='type'>(<tt><span class='object_link'><a href="Backends/Base.html" title="Elephas::Backends::Base (class)">Backends::Base</a></span></tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p>The backend to use for the check.</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Boolean</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'><p><code>true</code> if the entry is still valid, <code>false</code> otherwise.</p>
</div>
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


56
57
58</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/elephas/entry.rb', line 56</span>

<span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid_backend'>backend</span><span class='rparen'>)</span>
  <span class='id identifier rubyid_backend'>backend</span><span class='period'>.</span><span class='id identifier rubyid_now'>now</span> <span class='op'>-</span> <span class='id identifier rubyid_updated_at'>updated_at</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_ttl'>ttl</span> <span class='op'>/</span> <span class='int'>1000</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>

    <div id="footer">
  Generated on Sat Jul 20 13:31:56 2013 by
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.8.6.2 (ruby-1.9.3).
</div>

  </body>
</html>