EzDevInfo.com

redis-objects

Map Redis types directly to Ruby objects

Redis with Rails

I'm using Redis objects hash_key, in order to save a lot of email addresses. The key has set to a specific model.

  hash_key :emails, :global => true

If I use the following:

Committee.emails[1] = "mail@one.us, mail@two.com"

How long can my assigned email string become? I'd like to store a lot of addresses in this perhaps tens of thousands. Does this even make sense?


Source: (StackOverflow)

Redis-Objects in rails saves as String, not array

I am using the redis-objects gem to store simple info

class Purchase < ActiveRecord::Base
  include Redis::Objects
  hash_key :user_purchases, :marshal => true, :global => true  # "hash" is taken by Ruby

  def self.add_user_end(fb_id,item_id)
    if self.user_purchases[fb_id]
      a = Array.new
      a << candidate_id 
      self.user_purchases[fb_id] = a
    else
      new_a = self.user_purchases[fb_id]
      new_a << item_id
      self.user_purchases[fb_id] = new_a
    end
  end
end

I'm creating a method to collect user_purchases as a hasy_key, keyed by a users fb id. I would imply like to see a collection of ids when I use Purchase.user_purchases["2"] => [1,23,563,2]

I am running into a problem with Redis::Object where I can only save this as a string: Why?

1.9.3-p125 :050 > Purchase.user_purchases["6"].class
 => String 
1.9.3-p125 :051 > Purchase.user_purchases["6"]
 => "\u0004\b[\u0006I\"\v543555\u0006:\u0006ET" 

Answer:

My Initialization file was missing require 'redis/list'

require 'redis'
require 'redis/objects'
require 'redis/hash_key'
require 'redis/list'

Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)

Source: (StackOverflow)

Advertisements

Redis-objects to count the number of Model instances by a column on my model

I'm trying to understand how to use the Redis-Objects gem.

My goal is to Count the number of Purchase model instances by customer_id. For example, a customer loads a page and I would like to see the number of purchases by that Customer. To be clear the Purchase model has a customer_id column. Because of the volume of hits, I would like to use redis to relieve pressure on my server.

Does one use set to accomplish this task? How does one use the redis-objects gem to accomplish this task?


Source: (StackOverflow)

Redis Object - Get All Objects from DB - Profile View Counter

I am currently developing a membership website that include a view counter. Past experience says, having view counters in SQL is costly. I in fact kept away from view counters but today its not an option.

The project uses

  • Rails 4.0.2
  • Redis Objects gem
  • For demonstration I am hoping to use Heroku with Redis To Go plugin
  • Currently the counter is based on PG ( Active Record )
  • Redis Objects have been used with AR to count ( but how to save to AR profiles table ? )

Need / Thinking of achieving

  • Counts in Redis and periodically stores to PG table possibly using a Rake + schedule task
  • Efficiency

Problem

  • I can't figure out how to do a query the Redis DB to find all Profile objects in it.

If I could get this list of objects I can write a rake task to iterate through each item and save/update the modal value to the database.

After some search KEYS profile:* seems to be the only way to get all the profile values saved to the database. Then from it I have to manually fetch objects and update the values.

Question

  1. Is using keys to find the keys and then the objects sensible ( in terms of efficiency ) to use in a scheduled task possibly once a day.
  2. Is there a way to fetch all Profile objects directly from Redis db like we can do Profile.all in ActiveRecord ?
  3. Any suggestions to implement a counter is highly appreciated ( even if not Redis based )

-

class Profile < ActiveRecord::Base

  # To handle the profile counter
  include Redis::Objects
  counter :tviews

  # Associations

  belongs_to  :user

  # Other attributes
end

profiles_controller.rb

class ProfilesController < ApplicationController

  def public 
    @profile.tviews.increment
    # @profile.save # Save of AR based counting
  end

end

In the SLIM

    p.text-center#profile-counter
      | Views - 
      = @profile.tviews.value + @profile.views

Source: (StackOverflow)

Ruby on Rails always pointing local redis Server

I am using Redis with my ruby on rails application, To map Ruby object with Redis using redis-objects, dm-core and dm-redis-adapter. Below are the code snipts

Gemfile

gem 'redis-objects'
gem "dm-core", "~> 1.2.1"
gem "dm-redis-adapter"

/config/initializers/redis.rb

// LOCAL REDIS SERVER
Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)
// REMOTE REDIS SERVER
#Redis.current = Redis.new(:host => '<VM IP>', :port => <VM PORT>, :password => '<PASSWORD>')

Model.rb

DataMapper.setup(:default, {:adapter  => "redis"})

class User
  include Redis::Objects
  include DataMapper::Resource

  include ActiveModel::Validations  
  include ActiveModel::Conversion  

  # datamapper fields, just used for .create
  property :id, Serial
  property :name, String
  property :email, String
  property :des, Text

  def id
    1
  end

end

User.finalize

It's working fine for local redis server. Why app always pointing local redis, Even when providing remote host and port?

SOLVED: checkout my answer.


Source: (StackOverflow)

Redis-objects list on a Redis-object in ruby

I have a redis-object's list. Each object in the list needs to have another list.

class Parent include Redis::Objects list :children, marshal: true end

class children include Redis::Objects end

The above code samples just highlight the relationship. They do include other things like attr_accessors etc.

My issue arrives when I call children on an instance of the parent, such as in the example below:

p = Parent.new

Redis::List.new('some_key', marshal: true) << p

p.children => nil

I have a working example using an active record model with the list added to it. However including it on a redis-object seems to give me issues.

Do I need to handle the situation manually in the way it is described in part two of the redis-object documentation, or do I some how have to create the redis-objects list on creation of the parent object? My thoughts were that this creation was implicit


Source: (StackOverflow)