Skip to content

Commit 969b426

Browse files
committed
Renamed lock method to redis_lock to avoid conflict with ActiveRecord per #196
1 parent f9d7526 commit 969b426

4 files changed

Lines changed: 58 additions & 10 deletions

File tree

README.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,56 @@ Redis::Objects - Map Redis types directly to Ruby objects
55
[![Code Coverage](https://codecov.io/gh/nateware/redis-objects/branch/master/graph/badge.svg)](https://codecov.io/gh/nateware/redis-objects)
66
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MJF7JU5M7F8VL)
77

8-
**IMPORTANT: redis-objects 2.0.0 introduces a backwards incompatible change to key naming to fix a longstanding bug. For more information see [issue #213](https://github.com/nateware/redis-objects/issues/231)**
8+
Important 2.0 changes
9+
---------------------
10+
Redis::Objects 2.0 introduces several important backwards incompatible changes.
11+
Currently 2.0 can be installed with `gem install redis-objects --pre` or by listing it
12+
explicitly in your Gemfile:
13+
~~~ruby
14+
# Gemfile
15+
gem 'redis-objects', '>= 2.0.0.alpha'
16+
~~~
17+
You're encouraged to try it out in test code (not production) to ensure it works for you.
18+
Official release is expected later in 2022.
19+
20+
Key Naming Changes
21+
==================
22+
The internal key naming scheme has changed for `Nested::Class::Namespaces` to fix a longstanding bug.
23+
**This means your existing data in Redis will not be accessible until you call `migrate_redis_legacy_keys`.**
24+
25+
To fix this (only needed once), create a script like this:
26+
27+
~~~ruby
28+
class YouClassNameHere < ActiveRecord::Base
29+
include Redis::Objects
30+
# ... your relevant definitions here ...
31+
end
32+
33+
YourClassName.migrate_redis_legacy_keys
34+
~~~
935

36+
Then, you need to find a time when you can temporarily pause writes to your redis server
37+
so that you can run that script. It uses `redis.scan` internally so it should be able to
38+
handle a high number of keys. For large data sets, it could take a while.
39+
40+
For more details on the issue and fix refer to [#213](https://github.com/nateware/redis-objects/issues/231).
41+
42+
Rename of `lock` Method
43+
=======================
44+
The `lock` method that collided with `ActiveRecord::Base` has been renamed `redis_lock`.
45+
This means your classes need to be updated to call `redis_lock` instead:
46+
47+
~~~ruby
48+
class YouClassNameHere < ActiveRecord::Base
49+
include Redis::Objects
50+
redis_lock :mylock # formerly just "lock"
51+
end
52+
~~~
53+
54+
For more details on the issue and fix refer to [#213](https://github.com/nateware/redis-objects/issues/231).
55+
56+
Overview
57+
--------
1058
This is **not** an ORM. People that are wrapping ORM’s around Redis are missing the point.
1159

1260
The killer feature of Redis is that it allows you to perform _atomic_ operations
@@ -121,7 +169,7 @@ Here's an example that integrates several data types with an ActiveRecord model:
121169
class Team < ActiveRecord::Base
122170
include Redis::Objects
123171

124-
lock :trade_players, :expiration => 15 # sec
172+
redis_lock :trade_players, :expiration => 15 # sec
125173
value :at_bat
126174
counter :hits
127175
counter :runs
@@ -526,7 +574,7 @@ Locks work similarly. On completion or exception the lock is released:
526574

527575
~~~ruby
528576
class Team < ActiveRecord::Base
529-
lock :reorder # declare a lock
577+
redis_lock :reorder # declare a lock
530578
end
531579

532580
@team.reorder_lock.lock do
@@ -550,7 +598,7 @@ lock time.
550598

551599
~~~ruby
552600
class Team < ActiveRecord::Base
553-
lock :reorder, :expiration => 15.minutes
601+
redis_lock :reorder, :expiration => 15.minutes
554602
end
555603
~~~
556604

lib/redis/objects/locks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def self.included(klass)
1414
module ClassMethods
1515
# Define a new lock. It will function like a model attribute,
1616
# so it can be used alongside ActiveRecord/DataMapper, etc.
17-
def lock(name, options={})
17+
def redis_lock(name, options={})
1818
options[:timeout] ||= 5 # seconds
1919
lock_name = "#{name}_lock"
2020
redis_objects[lock_name.to_sym] = options.merge(:type => :lock)

lib/redis/objects/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Redis
22
module Objects
3-
VERSION = "1.7.0"
3+
VERSION = "2.0.0.alpha"
44
end
55
end

spec/redis_objects_model_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ class Roster
1010
counter :pitchers, :limit => :max_pitchers
1111
counter :basic
1212
hash_key :contact_information, :marshal_keys=>{'updated_at'=>true}
13-
lock :resort, :timeout => 2
13+
redis_lock :resort, :timeout => 2
1414
value :starting_pitcher, :marshal => true
1515
list :player_stats, :marshal => true
1616
set :outfielders, :marshal => true
1717
sorted_set :rank
18-
lock :per_field
18+
redis_lock :per_field
1919

2020
# global class counters
2121
counter :total_players_online, :global => true
2222
set :all_players_online, :global => true
2323
value :last_player, :global => true
24-
lock :nasty_global_mutex, :global => true # remember it appends "_lock"
24+
redis_lock :nasty_global_mutex, :global => true # remember it appends "_lock"
2525
sorted_set :global_player_leaderboard, :global => true
2626
hash_key :global_player_online_status, :global => true
2727

@@ -985,7 +985,7 @@ class CustomIdFieldRoster < UidRoster
985985
@roster.respond_to?(:extended_hash_key).should == false
986986

987987
LockRoster = Class.new(Roster)
988-
LockRoster.lock :extended
988+
LockRoster.redis_lock :extended
989989
extended_roster = LockRoster.new
990990
extended_roster.resort_lock.should.be.kind_of(Redis::Lock)
991991
extended_roster.extended_lock.should.be.kind_of(Redis::Lock)

0 commit comments

Comments
 (0)