Sha256: 6620417d3df096075fe9d34b75e1bd505ca863fdd927483284ab657bb50ecefc
Contents?: true
Size: 1.49 KB
Versions: 1
Compression:
Stored size: 1.49 KB
Contents
# This is an example of storing binary data. # # It will cache a test image, read it back, # and write it to disk as test_copy.jpg. require 'momento' # Get your Momento token from an environment variable. TOKEN = ENV.fetch('MOMENTO_AUTH_TOKEN') # Cached items will be deleted after 12.5 seconds. TTL_SECONDS = 12.5 # The name of the cache to create *and delete* CACHE_NAME = ENV.fetch('MOMENTO_CACHE_NAME') # So it can be run from the top of the repo # or from the examples directory. FILE_LOCATIONS = [ "spec/support/assets/test.jpg", "../spec/support/assets/test.jpg" ].freeze # Instantiate a Momento client. client = Momento::SimpleCacheClient.new( auth_token: TOKEN, default_ttl: TTL_SECONDS ) # Create a cache for testing, or use an already existing one. response = client.create_cache(CACHE_NAME) raise response.error if response.error? # Read an image file. file = FILE_LOCATIONS.find { |f| File.exist?(f) } contents = File.read(file) # Add the image to the cache. puts "Caching #{file}" client.set(CACHE_NAME, "test.jpg", contents) raise response.error if response.error? puts "Retrieving the image" response = client.get(CACHE_NAME, "test.jpg") raise "Cache image was not found!" if response.miss? raise response.error if response.error? puts "Writing the image as test_copy.jpg" f = File.open("test_copy.jpg", "wb") f.write(response.value_bytes) f.close # Delete our test cache. response = client.delete_cache(CACHE_NAME) raise response.error if response.error? puts "Now open test_copy.jpg"
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
momento-0.2.0 | examples/file.rb |