x_runtime 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -13,7 +13,8 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
13
13
  * 最近一次请求时间
14
14
  * 平均请求时间
15
15
 
16
- 当请求积累到50次后,通过pipeline机制一次将数据插入Redis,避免每次请求都访问一下Redis.
16
+ 当请求积累到50次后,通过pipeline机制一次将数据插入Redis,避免每次请求都访问一下Redis.
17
+ 引入了缓存超时机制后,当上一次将缓存写入时间和最新一次请求时间查过expire秒后,则将缓存写入Redis.
17
18
 
18
19
  ## Portal
19
20
 
@@ -22,14 +23,15 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
22
23
 
23
24
  ## Usage
24
25
 
25
- 引入这个middleware需要两个参数:
26
+ 引入这个middleware需要的参数:
26
27
 
27
28
  1. redis对象
28
- 2. :threshold,表示处理时间超过多少毫秒的请求才会被记录
29
- 3. :cache,表示请求积累到多少条的时候才通过redis的pipeline机制插入到redis数据库中
29
+ 2. :threshold,表示处理时间超过多少毫秒的请求才会被记录,默认100毫秒
30
+ 3. :cache,表示请求积累到多少条的时候才通过redis的pipeline机制插入到redis数据库中,默认50
31
+ 4. :expire,如果上一次写入Redis和这次请求相差时间大于expire,则在本次请求时也将缓存数据写入Redis,默认120秒
30
32
 
31
33
  可以指定XRuntime使用的Redis的key前缀或者叫命名空间:
32
- `XRuntime::NameSpace = "RuntimeEx::Threshold"`
34
+ `XRuntime::NameSpace = "XRuntime::Threshold"`
33
35
 
34
36
  ### Server
35
37
 
@@ -41,6 +43,8 @@ XRuntime::Server.use(Rack::Auth::Basic) do |user, password|
41
43
  end
42
44
  ```
43
45
 
46
+ 增加一个子页面`/incache`,可以查看缓存中没有插入Redis的数据,但要记得这些缓存数据是在每个Server各自的进程中保存,每次访问这个页面,得到的缓存数据只代表该进程正在缓存的数据。
47
+
44
48
  ### Sinatra
45
49
 
46
50
  收集数据 `config.ru`:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.7.0
@@ -1,7 +1,8 @@
1
1
  module XRuntime
2
2
  class DataSet
3
+ attr_accessor :data
3
4
 
4
- def initialize(key, script, count)
5
+ def initialize(key, script, count, expire)
5
6
  raise ArgumentError, "Script must not nil and be valid!" unless script
6
7
  @key = key
7
8
  @key_counter = "#{@key}::Counter"
@@ -9,6 +10,8 @@ module XRuntime
9
10
  @key_average = "#{@key}::Average"
10
11
  @script = script
11
12
  @count = count
13
+ @expire = expire
14
+ @expired_at = Time.now.to_i
12
15
  # 预先加载Lua脚本
13
16
  @script.sha
14
17
  @data = []
@@ -46,7 +49,9 @@ module XRuntime
46
49
  def add(member, score)
47
50
  @data.push([member, score])
48
51
  # 如果@data数据达到一定数量,则一起插入redis
49
- if @data.size >= @count
52
+ # 如果上一次写入Redis和这次请求相差时间大于@expire,则在本次请求时也将缓存数据写入Redis
53
+ if (@data.size >= @count) or (Time.now.to_i - @expired_at >= @expire)
54
+ @expired_at = Time.now.to_i
50
55
  @script.redis.multi do
51
56
  while (data = @data.pop) do
52
57
  @script.evalsha([@key], [data[0], data[1]])
@@ -5,14 +5,15 @@ module XRuntime
5
5
  def initialize(app, redis, opts = {})
6
6
  @app = app
7
7
  @redis = redis
8
- opts = {:threshold => 100.0, :cache => 50}.update opts
8
+ opts = {:threshold => 100.0, :cache => 50, :expire => 120}.update opts
9
9
  @cache = opts[:cache].to_i
10
+ @expire = opts[:expire].to_i
10
11
  @threshold = opts[:threshold].to_f
11
12
  XRuntime.middleware = self
12
13
  end
13
14
 
14
15
  def ds
15
- @ds ||= DataSet.new(redis_key, script, @cache)
16
+ @ds ||= DataSet.new(redis_key, script, @cache, @expire)
16
17
  end
17
18
 
18
19
  def script
@@ -6,9 +6,25 @@ end
6
6
 
7
7
  module XRuntime
8
8
  class Server < Sinatra::Base
9
+
10
+ before do
11
+ halt("Need use Rack::XRuntime first") unless XRuntime.middleware
12
+ end
13
+
9
14
  get '/' do
10
15
  @req = Rack::Request.new(env)
11
16
  Template.new(XRuntime.middleware.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render
12
17
  end
18
+
19
+ get '/incache' do
20
+ {
21
+ :status => 0,
22
+ :msg => "data in cache.",
23
+ :data => {
24
+ :count => XRuntime.middleware.ds.data.size,
25
+ :data => XRuntime.middleware.ds.data
26
+ }
27
+ }.to_json
28
+ end
13
29
  end
14
30
  end
data/lib/x_runtime.rb CHANGED
@@ -7,7 +7,7 @@ require "fileutils"
7
7
  $:.unshift(File.dirname(__FILE__))
8
8
 
9
9
  module XRuntime
10
- NameSpace = "RuntimeEx::Threshold"
10
+ NameSpace = "XRuntime::Threshold"
11
11
  extend self
12
12
  attr_accessor :middleware
13
13
 
data/test/server.ru CHANGED
@@ -8,7 +8,7 @@ require "#{File.dirname(__FILE__)}/../lib/x_runtime"
8
8
  class Server < Sinatra::Base
9
9
  # use XRuntime::Middleware, 10, Redis.connect(:url => "redis://localhost:6380/")
10
10
  # XRuntime::Middleware is same as Rack::XRuntime
11
- use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 50.0, :cache => 50
11
+ use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 50.0, :cache => 50, :expire => 60
12
12
 
13
13
  get /.*/ do
14
14
  # sleep(1)
data/x_runtime.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "x_runtime"
8
- s.version = "0.5.0"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["\u{5d14}\u{5ce5}"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x_runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: -1119447801023194543
138
+ hash: 1552083409244175477
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  none: false
141
141
  requirements: