如果我有一个代币在21天后到期
Rails.cache.write token, id, expires_in: 21.days
我正在尝试编写一个方法来检查是否已过期。请给我方向
def check_token_expiry(token)
end
当您在缓存时指定expires_in: 21.days
时,您会告诉底层缓存存储将给定密钥(此处为token
(的数据存储21天,21天后它过期意味着数据不再可用于给定密钥,您可以使用exist?
方法进行检查。
Rails.cache.exist?(token) # => true (data is available and you read)
Rails.cache.exist?(token) # => false (cache is expired, data is not available)
如果与token/key
关联的值是否过期,则将返回以下方法。
def check_token_expiry(token)
Rails.cache.read(token).nil?
end
基本上,尝试从缓存中读取值。如果它已过期,读取方法将返回nil。