# Expiration-date 1.1.0 (Screaming Bacon)

**URL:** <https://www.ruby-forum.com/t/expiration-date-1-1-0-screaming-bacon/146805>\
**Category:** Ruby\
**Created:** [August 25, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/expiration-date-1-1-0-screaming-bacon/146805 "2008-08-25T18:56:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 25, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/expiration-date-1-1-0-screaming-bacon/146805/1 "2008-08-25T18:56:39Z")

</div>

expiration-date version 1.1.0  
by Tim P.  
[http://codeforpeople.rubyforge.org/expiration-date](http://codeforpeople.rubyforge.org/expiration-date)  
(the “Screaming Bacon” release)

== DESCRIPTION

Really simple caching by attaching an expiration date to attributes.

The ExpirationDate module adss two methods to a class – expiring\_attr  
and  
expiring\_class\_attr. These two methods are used to declare attributes in  
the  
instance and in the class, respectively, that will expire after some  
period of  
seconds have elapsed. The attribute is re-initialized from the given  
block  
after it has expired. This is a very simple form of caching.

== CHANGES

- 1 minor enhancement
  - added expiring class attributes

== SYNOPSIS

A simple example demonstrating how the block gets called after the  
expiration  
time is passed.

class A  
include ExpirationDate  
expiring\_attr( :foo, 60 ) { ‘foo’ }  
end

a = A.new  
a.foo #=\> ‘foo’  
a.foo = ‘bar’  
a.foo #=\> ‘bar’

sleep 61  
a.foo #=\> ‘foo’

A slightly more useful example. Here we are going to extract information  
from a  
database every five minutes. This assumes you have the ‘activesupport’  
and  
‘activerecord’ gems installed.

class MyModel \< ::ActiveRecord::Base  
include ExpirationDate  
expiring\_class\_attr( :costly\_data, 5.minutes ) {  
models = MyModel.find( :all, :conditions =\> [‘costly query  
conditions’] )  
result = models.map {|m| # costly operations here}  
result  
}  
end

MyModel.costly\_data #=\> result

Attributes can be expired manually, and the time it takes them to expire  
can be  
modified as well.

class AgeDemo  
include ExpirationDate  
expiring\_attr( :bar, 120 ) { Time.now }  
end

demo = AgeDemo.new  
demo.bar #=\> now

sleep 60  
demo.bar #=\> 60 seconds ago

demo.expire\_bar\_now  
demo.bar #=\> now

demo.alter\_bar\_age( 10 )  
demo.expire\_bar\_now  
demo.bar #=\> now  
sleep 11  
demo.bar #=\> now

Blessings,  
TwP
