{"id":216047,"date":"2014-03-17T12:06:03","date_gmt":"2014-03-17T08:06:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=216047"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=216047","title":{"rendered":"<span class=\"post_title\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Redis EXPIRE \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u043e\u043d\u043b\u0430\u0439\u043d-\u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u0438 \u0432 Rails<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<habracut\/>  <\/p>\n<h3>\u041a\u043e\u043c\u0443<\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0422\u0435\u043c \u043a\u0442\u043e \u043d\u0430 <code>Ruby on Rails<\/code><\/li>\n<li>\u041a\u0442\u043e \u0436\u0435\u043b\u0430\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0442\u043e \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d, \u043d\u043e \u0435\u0449\u0451 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0441\u044f \u043a\u0430\u043a<\/li>\n<li>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Redis<\/code> (\u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0445\u043e\u0441\u0442\u0438\u043d\u0433\u0430 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440)<\/li>\n<\/ul>\n<p>  &nbsp;&nbsp;&nbsp;&nbsp;\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u0432 \u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438\u043b\u0438 \u043d\u0435\u0442 \u2014 \u044d\u0442\u043e <u><s>\u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435<\/s> \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e<\/u> \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u0438 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u0430 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0443\u0437\u043d\u0430\u0442\u044c \u0435\u0433\u043e (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f) \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 \u2014 \u0441\u0432\u0435\u0440\u043a\u0430 \u0441 \u044d\u0442\u043e\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u043e\u0439. \u041a\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u2014 \u0440\u0435\u0448\u0430\u0442\u044c \u0412\u0430\u043c, \u043d\u043e \u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u044f \u2014 \u043f\u0440\u043e\u0441\u0442 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>SQL<\/code> \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>Redis<\/code> \u0438 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0435\u0433\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u2014 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 (<code>expire<\/code>).<\/p>\n<h2>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<h3>\u0418\u043d\u0438\u0442<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># config\/initializers\/redis.rb      $redis_onlines = Redis.new <\/code><\/pre>\n<p>  \u0412\u044b\u0448\u0435 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u043e \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439<\/p>\n<pre><code class=\"ruby\"># config\/initializers\/redis.rb  $redis_onlines = Redis.new path: &quot;\/tmp\/redis.sock&quot;, db: 15, driver: :hiredis <\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><code>path: &quot;\/tmp\/redis.sock&quot;<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>socket<\/code>-\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e<\/li>\n<li><code>driver: :hiredis<\/code> \u2014 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 <code>hiredis<\/code> \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/li>\n<li><code>db: 15<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0443\u043b\u0435\u0432\u0430\u044f, \u043d\u043e \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0451 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0447\u0435\u0433\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u041d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0441\u0443\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u043f\u043e\u0434 \u043e\u043d\u043b\u0430\u0439\u043d-\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438 \u0434\u043b\u044f \u0447\u0435\u0433\u043e. <\/li>\n<\/ul>\n<p>  <\/p>\n<h3>Gemfile<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># Gemfile  gem 'redis' gem 'hiredis' # optional <\/code><\/pre>\n<p>  \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>bundle<\/code><\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432 <code>online<\/code><\/h3>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043c\u0435\u0442\u043e\u0434 current_user<\/b><\/p>\n<div class=\"spoiler_text\">\u041c\u0435\u0442\u043e\u0434 <code>current_user<\/code> \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0412\u0430\u043c\u0438 \u2014 \u044d\u0442\u043e \u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 <code>nil<\/code> \u2014 \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0432\u043e\u0448\u0451\u043b.  <\/p>\n<pre><code class=\"ruby\">def current_user   @current_user ||= User.find_by_id( session[ :user_id ] ) end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<pre><code class=\"ruby\"># app\/controllers\/application_controller.rb   after_filter :set_online   # \u0414\u043b\u044f Rails 4 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435:   # after_action :set_online   # \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c set_online    private      def set_online       if !!current_user         #  \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0432 pipelined \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f, \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u0439\u0434\u0443\u0442 \u043a\u0430\u043a \u043e\u0434\u0438\u043d \u0431\u0435\u0437 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f         # \u043e\u0442\u0432\u0435\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 multi-exec \u0432\u043c\u0435\u0441\u0442\u043e pipelined         # \u043d\u0435 \u0434\u0430\u0451\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u0440\u043e\u0441\u0442\u0430 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438         $redis_onlines.pipelined do           # \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447           $redis_onlines.set( current_user.id, nil )           # \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 - 10 \u043c\u0438\u043d\u0443\u0442, \u0447\u0435\u0440\u0435\u0437 10 \u043c\u0438\u043d\u0443 \u043a\u043b\u044e\u0447 \u0443\u0434\u0430\u043b\u0438\u0442\u044c\u0441\u044f           $redis_onlines.expire( current_user.id, 10*60 )         end       end     end <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0412 \u0441\u0435\u0442\u0438?<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\">    # app\/models\/user.rb          def online?       # \u0435\u0441\u043b\u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 \u0438\u0441\u0442\u0435\u043a\u043b\u043e - \u0442\u043e \u0432\u0435\u0440\u043d\u0451\u0442 false, \u0438\u043d\u0430\u0447\u0435 true       $redis_onlines.exists( self.id )     end <\/code><\/pre>\n<h3>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u043e\u043d\u0443\u0441 \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># app\/cpntrollers\/application_controller.rb      def all_who_are_in_touch       $redis_onlines.keys       # =&gt; [ &quot;123&quot;, &quot;234&quot;, &quot;1&quot;, &quot;23&quot; ]       # \u0432\u0435\u0440\u043d\u0451\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 id \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439     end <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0438 \u0432\u0441\u0451<\/p>\n<h4>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0435\u0440\u0435\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u043b\u044f anonymous<\/h4>\n<p>  &nbsp;&nbsp;&nbsp;&nbsp;\u0414\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0445 \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u0435\u0439 (\u0442\u0435\u0445 \u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f\/\u043d\u0435 \u0432\u043e\u0448\u0451\u043b) \u043f\u043e\u0434\u0445\u043e\u0434 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439, \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432 online<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\">    # app\/controllers\/application_controller.rb      def set_online   if !!current_user     $redis_onlines.pipelined do       # \u0432\u043e\u0448\u0435\u0434\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043a \u043a\u043b\u044e\u0447\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 &quot;user:&quot; \u043f\u0435\u0440\u0435\u0434 id       $redis_onlines.set( &quot;user:#{current_user.id}&quot;, nil )       $redis_onlines.expire( &quot;user:#{current_user.id}&quot;, 10*60 )     end   else     $redis_onlines.pipelined do       # \u043d\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 &quot;ip:&quot; \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e id \u0430\u0434\u0440\u0435\u0441       $redis_onlines.set( &quot;ip:#{request.remote_ip}&quot;, nil )       $redis_onlines.expire( &quot;ip:#{request.remote_ip}&quot;, 10*60 )     end   end end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0432 \u0441\u0435\u0442\u0438?<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\"># app\/models\/user.rb  def online?   $redis_onlines.exists( &quot;user:#{self.id}&quot; ) end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0441\u0435\u0442\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\"># app\/cpntrollers\/application_controller.rb        # \u0432\u0441\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043e\u043d\u043b\u0430\u0439\u043d (\u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0438\u0445 id)   def all_signed_in_in_touch     $redis_onlines.scan_each( match: 'user*' ).to_a   end    # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d   def all_anonymous_in_touch     $redis_onlines.scan_each( match: 'ip*' ).to_a.size   end    # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d   def all_who_are_in_touch     $redis_onlines.dbsize   end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h4>\u041d\u0443 \u0438 \u0441\u043e\u0432\u0441\u0435\u043c \u0447\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">9000+9000<\/b><\/p>\n<div class=\"spoiler_text\">Redis \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u043f\u043b\u043e\u0445\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u0443\u0441\u0442\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b <code>FLUSHALL<\/code> \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c) \u0438 \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 <a href=\"http:\/\/github.com\/logrusorgru\/any\/blob\/master\/redis_onlines_db_size\/test.rb\">\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 ruby<\/a>. \u0414\u043b\u044f 9000 \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 9000 \u043e\u043d\u043b\u0430\u0439\u043d \u0430\u043d\u043e\u043d\u0438\u043c\u0443\u0441\u043e\u0432 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\u043f\u0443\u0441\u0442\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445: 810.75K<\/li>\n<li>18000 \u0437\u0430\u043f\u0438\u0441\u0435\u0439: 3.49M<\/li>\n<\/ol>\n<p>  \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0434\u043b\u044f 65000 + 65000<\/p>\n<ol>\n<li>130000 \u0437\u0430\u043f\u0438\u0441\u0435\u0439: 18.66M<\/li>\n<\/ol>\n<p>  <\/div>\n<\/div>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0436\u0435\u0447\u043a\u043e \u0441\u0441\u044b\u043b\u043e\u043a \u043f\u043e \u0442\u0435\u043c\u0435 (<i>\u0430\u043d\u0433\u043b.<\/i>):<\/p>\n<ul>\n<li><a href=\"http:\/\/redis.io\/\">Redis<\/a><\/li>\n<li><a href=\"http:\/\/redis.io\/topics\/pipelining\">\u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 <code>pipelined<\/code><\/a><\/li>\n<li><a href=\"http:\/\/redis.io\/commands\/set\">set<\/a> | <a href=\"http:\/\/redis.io\/commands\/exists\">exists<\/a> | <a href=\"http:\/\/redis.io\/commands\/expire\">expire<\/a> | <a href=\"http:\/\/redis.io\/commands\/scan\">scan<\/a><\/li>\n<li><a href=\"http:\/\/rdoc.info\/github\/redis\/redis-rb\/Redis#scan_each-instance_method\">scan_each<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/redis\/redis-rb\">redis-rb<\/a><\/li>\n<li><a href=\"http:\/\/jimneath.org\/2011\/03\/24\/using-redis-with-ruby-on-rails.html\">\u043d\u0435\u043f\u043b\u043e\u0445\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043f\u043e Rails + Redis<\/a><\/li>\n<\/ul>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/216047\/\"> http:\/\/habrahabr.ru\/post\/216047\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<habracut\/>  <\/p>\n<h3>\u041a\u043e\u043c\u0443<\/h3>\n<p>  <\/p>\n<ul>\n<li>\u0422\u0435\u043c \u043a\u0442\u043e \u043d\u0430 <code>Ruby on Rails<\/code><\/li>\n<li>\u041a\u0442\u043e \u0436\u0435\u043b\u0430\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0442\u043e \u0438\u0437 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d, \u043d\u043e \u0435\u0449\u0451 \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0441\u044f \u043a\u0430\u043a<\/li>\n<li>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Redis<\/code> (\u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0445\u043e\u0441\u0442\u0438\u043d\u0433\u0430 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440)<\/li>\n<\/ul>\n<p>  &nbsp;&nbsp;&nbsp;&nbsp;\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u0430 \u0432 \u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438\u043b\u0438 \u043d\u0435\u0442 \u2014 \u044d\u0442\u043e <u><s>\u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435<\/s> \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e<\/u> \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u0438 \u043f\u0440\u0438 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043a \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u0430 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0443\u0437\u043d\u0430\u0442\u044c \u0435\u0433\u043e (\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f) \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 \u2014 \u0441\u0432\u0435\u0440\u043a\u0430 \u0441 \u044d\u0442\u043e\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043a\u043e\u0439. \u041a\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u2014 \u0440\u0435\u0448\u0430\u0442\u044c \u0412\u0430\u043c, \u043d\u043e \u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u044f \u2014 \u043f\u0440\u043e\u0441\u0442 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>SQL<\/code> \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>Redis<\/code> \u0438 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0435\u0433\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 \u2014 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 (<code>expire<\/code>).<\/p>\n<h2>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<h3>\u0418\u043d\u0438\u0442<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># config\/initializers\/redis.rb      $redis_onlines = Redis.new <\/code><\/pre>\n<p>  \u0412\u044b\u0448\u0435 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u043e \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439<\/p>\n<pre><code class=\"ruby\"># config\/initializers\/redis.rb  $redis_onlines = Redis.new path: &quot;\/tmp\/redis.sock&quot;, db: 15, driver: :hiredis <\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li><code>path: &quot;\/tmp\/redis.sock&quot;<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>socket<\/code>-\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e<\/li>\n<li><code>driver: :hiredis<\/code> \u2014 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 <code>hiredis<\/code> \u0431\u044b\u0441\u0442\u0440\u0435\u0435<\/li>\n<li><code>db: 15<\/code> \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0443\u043b\u0435\u0432\u0430\u044f, \u043d\u043e \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0451 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0447\u0435\u0433\u043e \u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0440\u0443\u0433\u043e\u0433\u043e. \u041d\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u2014 \u0441\u0443\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u043f\u043e\u0434 \u043e\u043d\u043b\u0430\u0439\u043d-\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438 \u0434\u043b\u044f \u0447\u0435\u0433\u043e. <\/li>\n<\/ul>\n<p>  <\/p>\n<h3>Gemfile<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># Gemfile  gem 'redis' gem 'hiredis' # optional <\/code><\/pre>\n<p>  \u041d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c <code>bundle<\/code><\/p>\n<h3>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432 <code>online<\/code><\/h3>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043c\u0435\u0442\u043e\u0434 current_user<\/b><\/p>\n<div class=\"spoiler_text\">\u041c\u0435\u0442\u043e\u0434 <code>current_user<\/code> \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0412\u0430\u043c\u0438 \u2014 \u044d\u0442\u043e \u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u043b\u0438 <code>nil<\/code> \u2014 \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0432\u043e\u0448\u0451\u043b.  <\/p>\n<pre><code class=\"ruby\">def current_user   @current_user ||= User.find_by_id( session[ :user_id ] ) end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<pre><code class=\"ruby\"># app\/controllers\/application_controller.rb   after_filter :set_online   # \u0414\u043b\u044f Rails 4 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435:   # after_action :set_online   # \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c set_online    private      def set_online       if !!current_user         #  \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0432 pipelined \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f, \u0434\u0432\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043f\u043e\u0439\u0434\u0443\u0442 \u043a\u0430\u043a \u043e\u0434\u0438\u043d \u0431\u0435\u0437 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f         # \u043e\u0442\u0432\u0435\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 multi-exec \u0432\u043c\u0435\u0441\u0442\u043e pipelined         # \u043d\u0435 \u0434\u0430\u0451\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u0440\u043e\u0441\u0442\u0430 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438         $redis_onlines.pipelined do           # \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447           $redis_onlines.set( current_user.id, nil )           # \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 - 10 \u043c\u0438\u043d\u0443\u0442, \u0447\u0435\u0440\u0435\u0437 10 \u043c\u0438\u043d\u0443 \u043a\u043b\u044e\u0447 \u0443\u0434\u0430\u043b\u0438\u0442\u044c\u0441\u044f           $redis_onlines.expire( current_user.id, 10*60 )         end       end     end <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0412 \u0441\u0435\u0442\u0438?<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\">    # app\/models\/user.rb          def online?       # \u0435\u0441\u043b\u0438 \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 \u0438\u0441\u0442\u0435\u043a\u043b\u043e - \u0442\u043e \u0432\u0435\u0440\u043d\u0451\u0442 false, \u0438\u043d\u0430\u0447\u0435 true       $redis_onlines.exists( self.id )     end <\/code><\/pre>\n<h3>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u043e\u043d\u0443\u0441 \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439<\/h3>\n<p>  <\/p>\n<pre><code class=\"ruby\"># app\/cpntrollers\/application_controller.rb      def all_who_are_in_touch       $redis_onlines.keys       # =&gt; [ &quot;123&quot;, &quot;234&quot;, &quot;1&quot;, &quot;23&quot; ]       # \u0432\u0435\u0440\u043d\u0451\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 id \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439     end <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0438 \u0432\u0441\u0451<\/p>\n<h4>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u043f\u0435\u0440\u0435\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u043b\u044f anonymous<\/h4>\n<p>  &nbsp;&nbsp;&nbsp;&nbsp;\u0414\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0445 \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u0435\u0439 (\u0442\u0435\u0445 \u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043b\u0441\u044f\/\u043d\u0435 \u0432\u043e\u0448\u0451\u043b) \u043f\u043e\u0434\u0445\u043e\u0434 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439, \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0432 online<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\">    # app\/controllers\/application_controller.rb      def set_online   if !!current_user     $redis_onlines.pipelined do       # \u0432\u043e\u0448\u0435\u0434\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043a \u043a\u043b\u044e\u0447\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 &quot;user:&quot; \u043f\u0435\u0440\u0435\u0434 id       $redis_onlines.set( &quot;user:#{current_user.id}&quot;, nil )       $redis_onlines.expire( &quot;user:#{current_user.id}&quot;, 10*60 )     end   else     $redis_onlines.pipelined do       # \u043d\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0435\u043c\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u0435\u0444\u0438\u043a\u0441 &quot;ip:&quot; \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e id \u0430\u0434\u0440\u0435\u0441       $redis_onlines.set( &quot;ip:#{request.remote_ip}&quot;, nil )       $redis_onlines.expire( &quot;ip:#{request.remote_ip}&quot;, 10*60 )     end   end end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0432 \u0441\u0435\u0442\u0438?<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\"># app\/models\/user.rb  def online?   $redis_onlines.exists( &quot;user:#{self.id}&quot; ) end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0432 \u0441\u0435\u0442\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"ruby\"># app\/cpntrollers\/application_controller.rb        # \u0432\u0441\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u043e\u043d\u043b\u0430\u0439\u043d (\u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0438\u0445 id)   def all_signed_in_in_touch     $redis_onlines.scan_each( match: 'user*' ).to_a   end    # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0435 \u0432\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d   def all_anonymous_in_touch     $redis_onlines.scan_each( match: 'ip*' ).to_a.size   end    # \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043e\u043d\u043b\u0430\u0439\u043d   def all_who_are_in_touch     $redis_onlines.dbsize   end <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<h4>\u041d\u0443 \u0438 \u0441\u043e\u0432\u0441\u0435\u043c \u0447\u0443\u0442\u044c \u0447\u0443\u0442\u044c \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">9000+9000<\/b><\/p>\n<div class=\"spoiler_text\">Redis \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u0435\u0442 \u043f\u043b\u043e\u0445\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u0443\u0441\u0442\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b <code>FLUSHALL<\/code> \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c) \u0438 \u0432\u043e\u0442 \u044d\u0442\u043e\u0442 <a href=\"http:\/\/github.com\/logrusorgru\/any\/blob\/master\/redis_onlines_db_size\/test.rb\">\u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 ruby<\/a>. \u0414\u043b\u044f 9000 \u043e\u043d\u043b\u0430\u0439\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 9000 \u043e\u043d\u043b\u0430\u0439\u043d \u0430\u043d\u043e\u043d\u0438\u043c\u0443\u0441\u043e\u0432 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\u043f\u0443\u0441\u0442\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445: 810.75K<\/li>\n<li>18000 \u0437\u0430\u043f\u0438\u0441\u0435\u0439: 3.49M<\/li>\n<\/ol>\n<p>  \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0434\u043b\u044f 65000 + 65000<\/p>\n<ol>\n<li>130000 \u0437\u0430\u043f\u0438\u0441\u0435\u0439: 18.66M<\/li>\n<\/ol>\n<p>  <\/div>\n<\/div>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0436\u0435\u0447\u043a\u043e \u0441\u0441\u044b\u043b\u043e\u043a \u043f\u043e \u0442\u0435\u043c\u0435 (<i>\u0430\u043d\u0433\u043b.<\/i>):<\/p>\n<ul>\n<li><a href=\"http:\/\/redis.io\/\">Redis<\/a><\/li>\n<li><a href=\"http:\/\/redis.io\/topics\/pipelining\">\u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 <code>pipelined<\/code><\/a><\/li>\n<li><a href=\"http:\/\/redis.io\/commands\/set\">set<\/a> | <a href=\"http:\/\/redis.io\/commands\/exists\">exists<\/a> | <a href=\"http:\/\/redis.io\/commands\/expire\">expire<\/a> | <a href=\"http:\/\/redis.io\/commands\/scan\">scan<\/a><\/li>\n<li><a href=\"http:\/\/rdoc.info\/github\/redis\/redis-rb\/Redis#scan_each-instance_method\">scan_each<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/redis\/redis-rb\">redis-rb<\/a><\/li>\n<li><a href=\"http:\/\/jimneath.org\/2011\/03\/24\/using-redis-with-ruby-on-rails.html\">\u043d\u0435\u043f\u043b\u043e\u0445\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043f\u043e Rails + Redis<\/a><\/li>\n<\/ul>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/216047\/\"> http:\/\/habrahabr.ru\/post\/216047\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-216047","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/216047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=216047"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/216047\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=216047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=216047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=216047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}