it 'should work' do
@replies = create_list(:reply, 3, user: user, topic: topic)
get "/api/v3/users/#{user.login}/replies.json", offset: 0, limit: 2
expect(json['replies'].size).to eq 2
fields = %w(id user body_html topic_id topic_title)
expect(json['replies'][0]).to include(*fields)
expect(json['replies'][0]['id']).to eq @replies[2].id
expect(json['replies'][0]['topic_title']).to eq topic.title
expect(json['replies'][1]['id']).to eq @replies[1].id
expect(response.body).to match_snapshot('users/:login/replies') # 其他信息快照记录
end
@hz_qiuyuanxin 测试更多的时候是希望及时的发现错误和潜在的错误
module MysqlConnection
extend ActiveSupport::Concern
included do
establish_connection ActiveRecord::Base.configurations[Rails.env]["xxx"]
end
# 如果是只读的话
def readonly?
true
end
end
class ModelName < ActiveRecord::Base
include MysqlConnection
end
default: &default
adapter: postgresql
encoding: unicode
pool: 5
xxx:
adapter: mysql2
pool: 5
timeout: 5000
database: xxxx
username: root
host: 123.123.123.123
password: xxxxx
development: &development
<<: *default
database: xxxxxxx
ruby-pinyin <= 0.4.8
#3 楼 @xlaok https://github.com/jrhe/grape-active_model_serializers/blob/master/lib%2Fgrape-active_model_serializers%2Fendpoint_extension.rb#L27
但是我还是建议你不要使用这样的插件,可以直接使用active_model_serializers
, 你的这个问题可以https://github.com/rails-api/active_model_serializers/blob/master/lib%2Factive_model%2Fserializer.rb#L148
可以直接出入参数的方式将 current_user 传入,像 @martin 所说.
调用的方式就是 如:https://github.com/rails-api/active_model_serializers#using-a-serializer-without-render
ActiveModel::SerializableResource.new(@message, scope: current_user).serializable_hash
active_model_serializers
默认会将 current_user
方法传入
def is_viewed
scope.is_viewed
end
如果你想将其他方法传入,https://github.com/rails-api/active_model_serializers/tree/0-9-2#customizing-scope
#7 楼 @42thcoder 离职后一段时间我发现确实之前有些地方是不对,不过很想听听你说的坑在哪里,这也有助于我的成长,谢谢。
#7 楼 @42thcoder 吐槽来听听呢
拍卖... 话说健身房会有游泳池吗。
其实还是 sql,
偷懒的话呢,就用 migration_comments
,
这个 gem 写了 adapter,
这样调用就可以了
klass = User
column_comments = klass.connection.retrieve_column_comments(klass.table_name)
返回是 字段 => 解释 的 hash
@rubyu2 先给你点个赞,存业务需要的两个时间戳, post_id
这个怎么算的
require "benchmark"
require "redis"
class MemoryWatcher
def self.measure(no_gc = true, current_redis, &block)
current_redis.flushdb
no_gc ? GC.disable : GC.start
memory_before = current_memory
gc_before = GC.stat
info_before = MemoryWatcher.redis_info(current_redis)
time = Benchmark.realtime { yield(current_redis) }
info_after = MemoryWatcher.redis_info(current_redis)
gc_after = GC.stat
GC.start unless no_gc
memory_after = current_memory
puts <<-INFOS
gc: #{no_gc ? "关闭" : "开启"}
time: #{time}
gc_count: #{gc_after[:count] - gc_before[:count]}
memory: #{(memory_after - memory_before).round(2)}M
redis used_memory: #{info_after['used_memory'].to_i - info_before['used_memory'].to_i}
INFOS
current_redis.flushdb
end
def self.current_memory
`ps -o rss= -p #{Process.pid}`.to_i/1024
end
def self.redis_info(current_redis)
current_redis.info
end
end
# 用户IDs 100_000_000..110_000_000
# 动态IDs 1_000_000..1_100_000
# 时间戳做 分值
user_ids = (100_000_000..100_001_000).to_a
post_ids = (100_000_000..100_000_100).to_a
timestamps = Time.now.to_i # 写死
current_redis = Redis.current
MemoryWatcher.measure(current_redis) do |redis|
redis.pipelined do
user_ids.each do |user_id|
post_ids.each do |post_id|
redis.ZADD "#{user_id}/#{post_id}", timestamps, timestamps
end
end
end
end
p '-' * 60
# 假定每个大v发一个 文章
v_ids = post_ids
MemoryWatcher.measure(current_redis) do |redis|
redis.pipelined do
# 不计算其他存储消耗, 因为这里没有存业务需要的两个时间戳, post_id
user_ids.each do |user_id|
v_ids.each do |v_id|
redis.SETBIT v_id, user_id, 1
end
end
end
end
我测出来,随着 id 的增大,setbit 会耗时变久,setbit 会超出值的范围. redis 内存使用 后者比前者多很多 喷喷看哪里有问题,谢谢
恩,存时间戳是更耗内存,不过如何解决 redis 收集大量内存处理 setbit 大 offset 情况?会不会堵塞?
求小伙伴一起啪啪啪....
你们为什么不早点来,我都发货了.........