网站消息实时推送与缓存处理

本文介绍Rails5中引入的ActionCable特性,用于实现实时WebSocket推送。此外,还探讨了通过Redis缓存消息记录并在客户端进行处理的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:在rails5中推出了ActionCable来实现消息的websoket推送
参考链接
[url]https://ruby-china.org/topics/28935[/url]
[url]https://ruby-china.org/topics/30494[/url]
[url]https://ruby-china.org/topics/28480[/url]

二:在某些使用场景下页可以考虑js定时轮询刷新的方式去做。(比如强制点击处理,消息才会不再弹出。)

下边分享一段,用redis缓存消息记录,当点击才会取消显示,但只有真正处理才会改变状态的代码。


# 数据库查询消息
def not_opration_message
_employee_id=current_employee.id
@not_opration_message = Message.where("employee_id=? and state=0",_employee_id) #查询所有未处理的消息
render :layout=> false
end

# 数据库处理消息
def deal_with_system_message
_employee_id=current_employee.id
_messages=Message.where("id=?",params[:id]) #查询所有未处理的消息
_message.update_all(:state=>1) #已处理
render :json => true
end

# 缓存处理消息
def delete_system_message
_employee_id=current_employee.id
redis=Redis::HashKey.new("user_#{_employee_id}")
redis.delete(params[:url])
render :json => true
end

# 缓存个人所有消息
def system_message
_employee_id=current_employee.id
redis=Redis::HashKey.new("user_#{_employee_id}")
if redis.blank?
render :json=> nil
else
render :json=> redis.to_json
end
end

#订单确认通知,创建消息并缓存
def self.tell_customer_order_confirm customer_order
customer_order_detail = customer_order.order_details_userful
customer_order_detail.each do |detail|
Message.create(message:"#{customer_order.order_no} 客户订单已确认,通知供应商发货", url:"/admin/order_list?order_no=#{customer_order.order_no}",state:0,employee_id:detail.emp)
message_hash = Redis::HashKey.new("user_#{detail.emp}")
message_hash["/admin/order_list?order_no=#{customer_order.order_no}"] = "#{customer_order.customer_order_no} 客户订单已确认,通知供应商发货"
end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值