Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/anemone/storage/redis.rb
Views: 11780
require 'redis'12module Anemone3module Storage4class Redis56MARSHAL_FIELDS = %w(links visited fetched)78def initialize(opts = {})9@redis = ::Redis.new(opts)10@key_prefix = opts[:key_prefix] || 'anemone'11keys.each { |key| delete(key) }12end1314def [](key)15rkey = "#{@key_prefix}:pages:#{key.to_s}"16rget(rkey)17end1819def []=(key, value)20rkey = "#{@key_prefix}:pages:#{key.to_s}"21hash = value.to_hash22MARSHAL_FIELDS.each do |field|23hash[field] = Marshal.dump(hash[field])24end25hash.each do |field, value|26@redis.hset(rkey, field, value)27end28end2930def delete(key)31rkey = "#{@key_prefix}:pages:#{key.to_s}"32page = self[key]33@redis.del(rkey)34page35end3637def each38rkeys = @redis.keys("#{@key_prefix}:pages:*")39rkeys.each do |rkey|40page = rget(rkey)41yield page.url.to_s, page42end43end4445def merge!(hash)46hash.each { |key, value| self[key] = value }47self48end4950def size51@redis.keys("#{@key_prefix}:pages:*").size52end5354def keys55keys = []56self.each { |k, v| keys << k.to_s }57keys58end5960def has_key?(key)61rkey = "#{@key_prefix}:pages:#{key.to_s}"62@redis.exists(rkey)63end6465def close66@redis.quit67end6869private7071def load_value(hash)72MARSHAL_FIELDS.each do |field|73unless hash[field].nil? || hash[field] == ''74hash[field] = Marshal.load(hash[field])75end76end77Page.from_hash(hash)78end7980def rget(rkey)81hash = @redis.hgetall(rkey)82if !!hash83load_value(hash)84end85end8687end88end89end909192