Giter Site home page Giter Site logo

memist's Introduction

Memist

Memist is a tiny library to help with memoizing values in Ruby classes. There are many other memoization libraries around but Memist provides more invalidation than others. Let's jump right into a demonstration.

class Person

  include Memist::Memoizable

  attr_accessor :first_name, :last_name, :date_of_birth

  def age
    Age.from_date_of_birth(date_of_birth)
  end
  memoize :age

  # Memist can also cache the results from methods that accept a single
  # argument. The cache will be based on the argument provided.
  def tweets(username)
    Twitter.tweets_for_username(username)
  end
  memoize :tweets

end

Here you can see we have a method called age which generates a person's age based on on their date_of_birth. It returns an integer. Each time we run this method, we don't want to go to the extra power of calcuating the age, so we want to cache the value for this instance. To do this, we just add the memoize method to our object.

person = Person.new
person.date_of_birth = Date.new(1986, 6, 10)
person.age      #=> 29 (retrieved by calculation)
person.age      #=> 29 (retrieved from the cache based)

Installation

Simple. Just add the gem to your Gemfile.

gem 'memist', '~> 2.0'

Usage

Firstly, include Memist::Memozation in any class you wish to add memoization to. Then, you need to specify which fields you want to be memoized in your objects. You do this by calling memoize as shown above.

Other handy methods

person = Person.new
person.date_of_birth = Date.new(1986, 6, 10)

# Access the raw underlying without any memoization
person.age_without_memoization    #=> 29

# Clear the cache for all methods on the model
person.flush_memoization

# Clear the cache for a single method on the model
person.flush_memoization(:age)

# Find out of a vale is memoized or not? When the value has been returned from
# memoized cache, it will be true otherwise it will be false.
person.memoized?(:age)    #=> false
person.memoized?(:age)    #=> true

# Disable memoization for all calls to memoized methods for a
# block of code
person.without_memoization do
  person.age                #=> 29 [no cache]
  person.memoized?(:age)    #=> false
  person.age                #=> 29 [no cache]
end

memist's People

Contributors

adamcooke avatar cabello avatar

Stargazers

Rohit Trivedi avatar Serhii Ponomarov avatar Bohdan Lytvyn avatar Michael Corrado avatar Chris Olstrom avatar Artem Kirienko avatar Chris avatar sniffer avatar Gabriel Klockner avatar Arian Deli avatar ReemmaMP avatar Alexander Tsirel avatar Nicolas Besnard avatar Zulhilmi Zainudin avatar Stanislav Fesenko avatar Thomas Klemm avatar Robert Audi avatar Stephan Kämper avatar Mike Lieser avatar Michaël Fischer avatar Eugene avatar  avatar Takahiro Mishiro avatar Kat Padilla avatar Karim El-Husseiny avatar  avatar Paul  avatar Markus Bengts avatar Dipesh Prajapati avatar lofwyr avatar Kevin Jalbert avatar Marten Klitzke avatar Rodrigo Rosenfeld Rosas avatar Brian Moseley avatar Harris Novick avatar Logan Leger avatar Benjamin Vetter avatar David Montesdeoca avatar jasdeep singh avatar Anton Semenov avatar Jakub Pavlík avatar Daniel Marchlik avatar Richardson Dackam avatar Matthew Werner avatar Víctor Pérez avatar Blake Hitchcock avatar Pete Taylor avatar Amit Thawait avatar  avatar Maksym Litvynov avatar Craig Sheen avatar Josh Powell avatar Hsiu-Fan Wang avatar Artur Trofimov avatar Johan avatar Lennard Timm avatar Alberto Colón Viera avatar Evgeniy Fateev avatar Valentino Stoll avatar Seamus Abshere avatar John C. Bland II avatar Nicolas Zermati avatar Jônatas avatar  avatar Arnaud Mesureur avatar Rodrigo Botafogo avatar Patrick Veverka avatar niedhui avatar Ferdy avatar Dmitry Polushkin avatar Vlad Andersen avatar Anurag Abbott avatar Ahmet Abdi avatar Dean Perry avatar Phuwanart Larpmark avatar  avatar

Watchers

Димыч avatar James Cloos avatar Craig Sheen avatar  avatar

Forkers

cabello aritode

memist's Issues

Memoization doesn't work

Hello,
I have class User. For persist layer I use Mongoid.
User:


class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia
........
  def license
    # calculating.... Return symbol
  end
  memoize :license
.....
end

Then I start rails console and do next operation:

[1] pry(main)> >> u  = User.last
#<User _id: 55e2e45f77656245c7020000, created_at: 2015-08-30 11:09:24 UTC, .......>
[2] pry(main)> >> u.license.memoized?
=> false
[3] pry(main)> >> u.license
=> :standard
[4] pry(main)> >> u.license.memoized?
=> false

Why memist doesn't memoizable method license ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.