class Racknga::CacheDatabase

This is a cache database based on groonga. It is used by Racknga::Middleware::Cache.

Normally, purge_old_responses is only used for cache maintenance.

Public Class Methods

new(database_path) click to toggle source

@param [String] database_path the path for cache database.

# File lib/racknga/cache_database.rb, line 30
def initialize(database_path)
  @database_path = database_path
  @context = Groonga::Context.new(:encoding => :none)
  ensure_database
end

Public Instance Methods

close_database() click to toggle source
# File lib/racknga/cache_database.rb, line 88
def close_database
  @database.close
end
configuration() click to toggle source
# File lib/racknga/cache_database.rb, line 44
def configuration
  configurations["default"]
end
configurations() click to toggle source
# File lib/racknga/cache_database.rb, line 40
def configurations
  @context["Configurations"]
end
ensure_database() click to toggle source
# File lib/racknga/cache_database.rb, line 78
def ensure_database
  if File.exist?(@database_path)
    @database = Groonga::Database.open(@database_path, :context => @context)
  else
    create_database
  end
  ensure_tables
  ensure_default_configuration
end
purge_old_responses() click to toggle source

Purges old responses. To clear old caches, you should call this method periodically or after data update.

You can call this method by the different process from your Rack application process. (e.g. cron.) It’s multi process safe.

# File lib/racknga/cache_database.rb, line 54
def purge_old_responses
  age_modulo = 2 ** 31 - 1
  age = configuration.age
  previous_age = (age - 1).modulo(age_modulo)
  next_age = (age + 1).modulo(age_modulo)

  target_responses = responses.select do |record|
    record.age == next_age
  end
  target_responses.each do |response|
    response.key.delete
  end
  configuration.age = next_age

  target_responses = responses.select do |record|
    record.age <= previous_age
  end
  target_responses.each do |response|
    response.key.delete
  end

  responses.defrag if responses.respond_to?(:defrag)
end
responses() click to toggle source
# File lib/racknga/cache_database.rb, line 36
def responses
  @context["Responses"]
end

Private Instance Methods

create_configurations_table() click to toggle source
# File lib/racknga/cache_database.rb, line 108
def create_configurations_table
  Groonga::Schema.define(:context => @context) do |schema|
    schema.create_table("Configurations",
                        :type => :hash,
                        :key_type => "ShortText") do |table|
      table.uint32("age")
    end
  end
end
create_database() click to toggle source
# File lib/racknga/cache_database.rb, line 118
def create_database
  FileUtils.mkdir_p(File.dirname(@database_path))
  @database = Groonga::Database.create(:path => @database_path,
                                       :context => @context)
end
create_responses_table() click to toggle source
# File lib/racknga/cache_database.rb, line 93
def create_responses_table
  Groonga::Schema.define(:context => @context) do |schema|
    schema.create_table("Responses",
                        :type => :hash,
                        :key_type => "ShortText") do |table|
      table.uint32("status")
      table.short_text("headers")
      table.text("body", :compress => :zlib)
      table.short_text("checksum")
      table.uint32("age")
      table.time("created_at")
    end
  end
end
ensure_default_configuration() click to toggle source
# File lib/racknga/cache_database.rb, line 129
def ensure_default_configuration
  configurations.add("default")
end
ensure_tables() click to toggle source
# File lib/racknga/cache_database.rb, line 124
def ensure_tables
  create_configurations_table
  create_responses_table
end