Skip to content

Instantly share code, notes, and snippets.

@bnagy
Created July 2, 2012 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnagy/3031219 to your computer and use it in GitHub Desktop.
Save bnagy/3031219 to your computer and use it in GitHub Desktop.
def tc_deflate!( set )
# Single thread / core only!
cur=@tc.size/2
cached={}
set.map! {|elem|
unless (idx=@tc[elem]) #already there
cur+=1
cached[cur]=elem
cached[elem]=cur
Integer( cur )
else
Integer( idx )
end
}
@tc.update cached
set
end
def redis_store( word )
# Safe concurrent access to the Redis DB
loop do
@redis.watch("words")
value = @redis.hget("words", word)
@redis.unwatch and break(value) if value
idx = (@redis.hlen("words")/2).to_s
break(idx) if @redis.multi do
@redis.hmset("words", word, idx, idx, word)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment