cycr 0.2.1 → 0.2.2

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.
@@ -1,3 +1,7 @@
1
+ 0.2.2
2
+ - Fix invalid use of ? expression
3
+ - Fix unscoped reference to Symbol in Cache
4
+ - Thread-safe query cache
1
5
  0.2.1
2
6
  - Introduce query cache
3
7
  0.2.0
@@ -1,3 +1,4 @@
1
+ require 'thread'
1
2
  require 'ref'
2
3
 
3
4
  module Cyc
@@ -5,6 +6,31 @@ module Cyc
5
6
  def initialize
6
7
  @soft_references = Ref::SoftValueMap.new
7
8
  @hard_references = {}
9
+ @in_progress = {}
10
+ @lock = Mutex.new
11
+ end
12
+
13
+ # Get cached value of a key or generate new one using given block
14
+ def cached_value(key)
15
+ monitor = value = nil
16
+ @lock.synchronize do
17
+ if @hard_references.has_key?(key)
18
+ return @hard_references[key]
19
+ elsif (value = @soft_references[key])
20
+ return value
21
+ elsif (monitor = @in_progress[key])
22
+ monitor.wait(@lock)
23
+ return self[key]
24
+ end
25
+ @in_progress[key] = monitor = ConditionVariable.new
26
+ end
27
+ value = yield
28
+ @lock.synchronize do
29
+ self[key] = value
30
+ @in_progress.delete key
31
+ monitor.broadcast
32
+ end
33
+ value
8
34
  end
9
35
 
10
36
  # Get a value from cache.
@@ -19,7 +45,7 @@ module Cyc
19
45
  # Put a value for a given key to the cache.
20
46
  def []=(key,value)
21
47
  case value
22
- when TrueClass,FalseClass,NilClass,Fixnum,Symbol
48
+ when TrueClass,FalseClass,NilClass,Fixnum,::Symbol
23
49
  @soft_references.delete(key)
24
50
  @hard_references[key] = value
25
51
  else
@@ -30,8 +56,10 @@ module Cyc
30
56
 
31
57
  # Clear the cache.
32
58
  def clear
33
- @soft_references.clear
34
- @hard_references.clear
59
+ @lock.synchronize do
60
+ @soft_references.clear
61
+ @hard_references.clear
62
+ end
35
63
  end
36
64
  end
37
65
  end
@@ -29,6 +29,11 @@ module Cyc #:nodoc:
29
29
  # (and calls based on it -- i.e. direct Cyc calls, e.g. cyc.genls :Dog).
30
30
  attr_accessor :cache_enabled
31
31
 
32
+ # The +cache+ instance of a client.
33
+ # You may call +clear+ on it to clear cache's content,
34
+ # You may copy it's reference to the other client instances.
35
+ attr_accessor :cache
36
+
32
37
  # The +host+ the client connects to.
33
38
  attr_reader :host
34
39
 
@@ -55,7 +60,7 @@ module Cyc #:nodoc:
55
60
  # - +:host+ = +localhost+ server address
56
61
  # - +:port+ = +3601+ server port
57
62
  # - +:debug+ = +false+ initial debug flag
58
- # - +:cache+ = +false+ initial cache enabled flag
63
+ # - +:cache+ = +false+ initial cache enabled flag or external cache instance
59
64
  # - +:timeout+ = +0.2+ connection timeout in seconds
60
65
  # - +:url+ (String): +cyc://host:port+ overrides +:host+, +:port+
61
66
  # - +:driver+ (Class) = Cyc::Connection::Socket client connection driver class
@@ -85,8 +90,9 @@ module Cyc #:nodoc:
85
90
  @timeout = (options[:timeout] || 0.2).to_f
86
91
  @driver = options[:driver] || Connection.driver
87
92
  @debug = !!options[:debug]
88
- @cache_enabled = !!options[:cache]
89
- @cache = Cache.new
93
+ cache = options[:cache]
94
+ @cache_enabled = !!cache
95
+ @cache = cache.respond_to?(:cached_value) ? cache : Cache.new
90
96
  @thread_safe = !!options[:thread_safe]
91
97
 
92
98
  if @thread_safe
@@ -149,15 +155,15 @@ module Cyc #:nodoc:
149
155
 
150
156
  # Sends the +messsage+ to the Cyc server and returns a parsed answer.
151
157
  def talk(message, options={})
152
- if @cache_enabled && @cache[message]
153
- return @cache[message]
154
- end
155
- send_message(message)
156
- result = receive_answer(options)
157
158
  if @cache_enabled
158
- @cache[message] = result
159
+ @cache.cached_value(message) do
160
+ send_message(message)
161
+ receive_answer(options)
162
+ end
163
+ else
164
+ send_message(message)
165
+ receive_answer(options)
159
166
  end
160
- result
161
167
  end
162
168
 
163
169
  # Sends the +message+ to the Cyc server and
@@ -1,3 +1,3 @@
1
1
  module Cyc
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cycr
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Aleksander Pohl
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-06-02 00:00:00 Z
14
+ date: 2012-06-14 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec