Sha256: eab52513d989e5d8d988581649dcaa6e00b6ca5f7ceee2d67c1402956f102a9d

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RailsWebCache
  class KeysController < RailsWebCache::ApplicationController
    before_action :set_key, only: %i[show destroy]
    before_action :set_page, only: :index
    before_action :set_per_page, only: :index
    before_action :set_offset, only: :index

    # GET /keys
    def index
      @keys = keys
      @pages = (@keys.size / @per_page.to_f).ceil
      @keys = @keys[@offset, @per_page] || []
      @keys = @keys.map.with_index do |key, idx|
        format_key(key, idx)
      end
    end

    # GET /keys/:key
    def show
      redirect_to root_path if @key.nil?
      @value = cache.read(@key)
      @entry = cache.entry(@key)
    end

    # DELETE /keys/:key
    def destroy
      redirect_to root_path if @key.nil?
      cache.delete(@key)
      redirect_to root_path
    end

    # DELETE /keys/all/:keys
    def destroy_all
      keys = params[:keys].presence || []
      redirect_to root_path if keys.nil? || keys.empty?
      keys.each do |key|
        cache.delete(key)
      end
      redirect_to root_path
    end

    private

    def cache
      @cache ||= RailsWebCache.cache
    end

    def format_key(key, idx)
      entry = cache.entry(key)
      {
        id: idx,
        key: key,
        value: cache.read(key),
        expires_at: entry&.expires_at,
        size: entry&.size
      }
    end

    def set_key
      @key = params[:key]
    end

    def set_page
      page = (params[:page].presence || 1).to_i
      page = 1 if page.negative? || page > (2**32)
      @page = page
    end

    def set_offset
      @offset = (@page - 1) * @per_page
    end

    def set_per_page
      @per_page = 20
    end

    def keys
      query = params[:query].presence
      if query.present?
        cache.search(query)
      else
        cache.keys
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_web_cache-0.7.6 app/controllers/rails_web_cache/keys_controller.rb
rails_web_cache-0.7.5 app/controllers/rails_web_cache/keys_controller.rb
rails_web_cache-0.7.4 app/controllers/rails_web_cache/keys_controller.rb
rails_web_cache-0.7.3 app/controllers/rails_web_cache/keys_controller.rb
rails_web_cache-0.7.2 app/controllers/rails_web_cache/keys_controller.rb