level 1
require_relative *book_in_stock*
require_relative *database*
require *dalli*
class DataAccess
def initialize db_path
@database= DataBase.new db_path
@Remote_cache = Dalli::Client.new(*localhost:11211*)
# Relevant data structure(s) for local cache
end
def start
@database.start
end
def stop
end
def findISBN isbn
book = nil
version = @Remote_cache.get "v_#{isbn}"
if version
serial = @Remote_cache.get "
#{version}_#
{isbn}"
book = BookInStock.from_cache serial
#@Remote_cache.set "#
{version + 1}_#{book.isbn}", book.to_cache
#@Remote_cache.set "v_#{book.isbn}",version+1
else # Not in cache, so add it.
book = @database.findISBN isbn
if book
@Remote_cache.set "v_#{book.isbn}",1
@Remote_cache.set "1_#{book.isbn}",book.to_cache
end
end
book
end
def authorSearch(author)
@database.authorSearch author
end
def updateBook book
@database.updateBook book
flushWhenChangeDataBase
end
def addBook book
@database.addBook book
end
def deleteBook isbn
@database.deleteBook isbn
flushWhenChangeDataBase
end
def flushWhenChangeDataBase
@Remote_cache = nil
end
end
这是其中的代码
2014年10月05日 21点10分