みんなのちからになりたい

コピペでブログラムつくっていきたい

cacheみたいなことしたい

スクレイピングとかするとき、毎回アクセスしにいくと遅いしそのまえに相手の鯖に悪そうなのでキャッシュしといて2回めからはそこから取ってくるみたいなことしたい。
serializeしてファイルに書けばいけそうなので調べたらMarshalでやれそう。

module Marshal

dumpでserializeしてloadで戻す感じっぽい。
やってみる。

p "=== Hash"
hash = { aaa: 123, bbb: 456}
p hash
cache = Marshal.dump hash
p cache
res = Marshal.load cache
p res
p ""

p "=== Array"
arr = ["a", 1, "b", 2]
p arr
cache = Marshal.dump arr
p cache
res = Marshal.load cache
p res
p ""

p "=== Hash and Array"
arr = ["a", 1, "b", 2]
hash = { aaa: 123, bbb: arr}
p hash
cache = Marshal.dump hash
p cache
res = Marshal.load cache
p res
p ""

p "=== Primitive"
pri = 100.9
p pri
cache = Marshal.dump pri
p cache
res = Marshal.load cache
p res
p ""

p "=== Object"
class Cache
   def initialize
     @aa = 1
   end
   def disp
    p @aa
   end
end
obj = Cache.new
p obj
cache = Marshal.dump obj
p cache
res = Marshal.load cache
p res
p ""


$ ruby cache.rb
"=== Hash"
{:aaa=>123, :bbb=>456}
"\x04\b{\a:\baaai\x01{:\bbbbi\x02\xC8\x01"
{:aaa=>123, :bbb=>456}
""
"=== Array"
["a", 1, "b", 2]
"\x04\b[\tI\"\x06a\x06:\x06EFi\x06I\"\x06b\x06;\x00Fi\a"
["a", 1, "b", 2]
""
"=== Hash and Array"
{:aaa=>123, :bbb=>["a", 1, "b", 2]}
"\x04\b{\a:\baaai\x01{:\bbbb[\tI\"\x06a\x06:\x06EFi\x06I\"\x06b\x06;\aFi\a"
{:aaa=>123, :bbb=>["a", 1, "b", 2]}
""
"=== Primitive"
100.9
"\x04\bf\n100.9"
100.9
""
"=== Object"
#<Cache:0x007fcc738bfcf8 @aa=1>
"\x04\bo:\nCache\x06:\b@aai\x06"
#<Cache:0x007fcc738bfbb8 @aa=1>
""

ファイルに書いてそれを戻してみる。

p "=== Hash and File Write"
File::open("./hash.dat", "a+") { |f|
  hash = { aaa: 123, bbb: 456}
  p hash
  Marshal.dump(hash, f)
}
cache = nil
File::open("./hash.dat", "r") {|f|
  cache = Marshal.load(f)
}
p cache
p ""

p "=== Array and File Write"
File::open("./arr.dat", "a+") { |f|
  arr = ["a", 1, "b", 2]
  p arr
  Marshal.dump(arr, f)
}
cache = nil
File::open("./arr.dat", "r") {|f|
  cache = Marshal.load(f)
}
p cache
p ""

p "=== Hash and Array and File Write"
File::open("./hasharray.dat", "a+") { |f|
  arr = ["a", 1, "b", 2]
  hash = { aaa: 123, bbb: arr}
  p hash
  Marshal.dump(hash, f)
}
cache = nil
File::open("./hasharray.dat", "r") {|f|
  cache = Marshal.load(f)
}
p cache
p ""

p "=== Primitive and File Write"
File::open("./primitive.dat", "a+") { |f|
  pri = 100.9
  p pri
  Marshal.dump(pri, f)
}
cache = nil
File::open("./primitive.dat", "r") {|f|
  cache = Marshal.load(f)
}
p cache
p ""

p "=== Object and File Write"
File::open("./object.dat", "a+") { |f|
  class Cache
     def initialize
       @aa = 1
     end
     def disp
      p @aa
     end
  end
  obj = Cache.new
  p obj
  Marshal.dump(obj, f)
}
cache = nil
File::open("./object.dat", "r") {|f|
  cache = Marshal.load(f)
}
p cache
p ""


$ ruby cache2.rb
"=== Hash and File Write"
{:aaa=>123, :bbb=>456}
{:aaa=>123, :bbb=>456}
""
"=== Array and File Write"
["a", 1, "b", 2]
["a", 1, "b", 2]
""
"=== Hash and Array and File Write"
{:aaa=>123, :bbb=>["a", 1, "b", 2]}
{:aaa=>123, :bbb=>["a", 1, "b", 2]}
""
"=== Primitive and File Write"
100.9
100.9
""
"=== Object and File Write"
#<Cache:0x007fe8d308b5d8 @aa=1>
#<Cache:0x007fe8d308b3f8 @aa=1>
""

これでいけそう。
勉強になる。