美文网首页
Grape 片段缓存

Grape 片段缓存

作者: orcl_zhang | 来源:发表于2015-07-18 11:26 被阅读56次

在git上找了很久没有找到一个grape的片段缓存方案,所以才有想法自己写一个。刚刚起步开始写,很多想法还未实现。但是已经可以配合rails使用了。
https://github.com/u2/grape-present_cache
使用方法:

module MyApi < Grape::API
  format :json

  include Grape::Present::Cache

  resources :posts do
    desc "Return a post"
    get ":id" do
      post = Post.find(params[:id])
      car_brands = CarBrand.where(popular: true).all
      present_cache(key: "api:posts:#{post.id}", expires_in: 2.hours) do
        present :post, post, with: API::Entities::Post
      end
      present_cache(key: "v1:api:app:cars:b", expires_in: 2.hours) do
        present car_brands, with: Entities::Car::CarBrand
      end
      present ApiMeta.new, with: Entities::Meta::Base
    end
  end
end

顺便分享一下平时grape缓存的一个小技巧:
技巧一:

      class Product < Grape::Entity
        root 'products', 'product'
        expose :id, :photo_path, :price, :width, :height
        expose :favorites_count, as: :like_count
        expose :like do |product, options|
          if options[:favorites]
            options[:favorites].map(&:product_id).include?(product.id)
          else
            false
          end
        end
      end

    class Products < Grape::API
      resource :products do

        desc "首页商品列表"
        params do
          optional :per_page, type: Integer, default: 20
          optional :sort, type: Integer, default: 0
          optional :page, type: Integer, default: 1
        end
        get do
          favorites = current_user.favorites if current_user
          case params[:sort].to_i
          when 0
            products = Product.ranking
          end
          products = products.paginate(:page => params[:page], :per_page => params[:per_page])
          present products, with: Entities::Common::Product, favorites: favorites
        end
      end

对于上面的api,返回的entity里like字段是根据登陆用户来判断的,这样不便于将这段代码缓存。可以将like字段从Product这个Entity里拿出来,然后增加一个字段like_ids,将有like的id单独返回。

present_cache(key: "v1:api:app:products:#{page}:{per_page}", expires_in: 2.hours)
  present products, with: Entities::Common::Product
end
present :like_ids, favorites.map(&:product_id)

相关文章

网友评论

      本文标题:Grape 片段缓存

      本文链接:https://www.haomeiwen.com/subject/akrpqttx.html