Forum: Ruby on Rails asp's application object rails equivalent?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sas S. (Guest)
on 2006-04-11 21:38
In asp there's a built'in object, Application, which behaves like the
session, but it allows you to share information among all users of the
application.

(http://www.microsoft.com/windows2000/en/server/IIs...)

It's just like if every user would be accessing the same session.

what is the rails equivalent?

And if there is no such thing, how would you implement such a thing?
(taking ito account that it would arouse some concurrency issues, in
fact asp's Application object has a lock and unlock method for that
purpose

Saludos

Sas
Steve R. (Guest)
on 2006-04-11 22:24
(Received via mailing list)
There are a number of load-once files. A logical place is
environment.rb.
Consider placing something like this at the end of environment.rb:

# Include your application configuration below

include 'thread'
mutex = Mutex.new

module Settings
  SETTINGS = {}
  SETTINGS[:additional_snippets] = 5

  def config(config_var)
    mutex.synchronize do
      SETTINGS[config_var]
    end
  end
end

Then in other code, include Settings and:

snippets = Settings::config(:additional_snippets)

I tested all except the mutex part.
--
View this message in context:
http://www.nabble.com/asp%27s-application-object-r...
Sent from the RubyOnRails Users forum at Nabble.com.
Steve R. (Guest)
on 2006-04-11 22:37
(Received via mailing list)
Sorry. The concurrency code I originally posted won't work. This appears
better.

# Include your application configuration below

require 'thread'

module Settings
  MUTEX = Mutex.new
  SETTINGS = {}
  SETTINGS[:additional_snippets] = 5

  def config(config_var)
    MUTEX.synchronize do
      SETTINGS[config_var]
    end
  end

  def config=(var)
    MUTEX.synchronize do
      SETTINGS[config_var] = var
    end
  end
end
--
View this message in context:
http://www.nabble.com/asp%27s-application-object-r...
Sent from the RubyOnRails Users forum at Nabble.com.
Phillip H. (Guest)
on 2006-04-12 02:26
(Received via mailing list)
On 12/04/2006, at 6:21 AM, s.ross wrote:

> There are a number of load-once files. A logical place is
> environment.rb.
> Consider placing something like this at the end of environment.rb:

Of course, that won't work with fastcgi, which uses a multi process
model, or an application server cluster. Either use the database or
Drb. I recommend the database, it's easier, just create a settings
table and use a before_filter to get the records. Sure it's an extra
query, but it shouldn't matter for a decent DBMS.

--
Phillip H.
removed_email_address@domain.invalid
http://www.sitharus.com/
Steve R. (Guest)
on 2006-04-12 03:33
(Received via mailing list)
This was one of the original suggestions and is, in fact, how I am doing
it.
I have a settings table with two fields:

setting_name, :string
setting_value, :string

Here is the model:

class Setting < ActiveRecord::Base
  def self.[](index)
    return self.find_by_setting_name(index.to_s).setting_value
  end

  def self.[]=(index, value)
    setting = self.find_by_setting_name(index.to_s)
    setting.update_attribute('setting_value', value)
  end
end

With this I can do:

number_of_snippets = Setting[:article_paragraphs].to_i

A possible enhancement is to keep some type information so the explicit
conversion is not required.
--
View this message in context:
http://www.nabble.com/asp%27s-application-object-r...
Sent from the RubyOnRails Users forum at Nabble.com.
Sas S. (Guest)
on 2006-04-12 05:18
Thanks for the prompt answers

In fact, I was thinking about some way to implement a quick and dirty
cache system, specially for those little combo boxes that takes so many
trips to the DB to fill.

in this thread I explain what I had n mind (when I coulnd't find the
original thread I thought I haven't posted it at all... sorry for the
duplication)

http://www.ruby-forum.com/topic/61795#new

So, what would be the best way to implement such a thing???

I guess that if I keep the cache relatively small -invalidating it on a
time-elapsed or in a fixed lenght basis-, I can load it all on the first
access to the cache, and then in one trip to the db I would get several
items. Even thought I would be going to the DB, it would be an
improvement (well, that depends on wheter retrieving a hundred items in
one trip is faster thatn retrieving ten in ten trips)

Saludos

Sas

PS: Before you tell me, I promess I'll study rails cache system ;-)
Steve R. (Guest)
on 2006-04-12 07:30
(Received via mailing list)
I'm in agreement with the poster who mentioned that optimizing should
wait
until you know you have a performance bottleneck. If you are really set
on a
cache, look into memcached
(http://weblog.rubyonrails.com/articles/2006/01/20/...).
It might fit the bill.
--
View this message in context:
http://www.nabble.com/asp%27s-application-object-r...
Sent from the RubyOnRails Users forum at Nabble.com.
This topic is locked and can not be replied to.