Last active
October 12, 2024 08:31
-
-
Save sreeram-venkitesh/de791423bc1e1ed3af7f3caade03bc3f to your computer and use it in GitHub Desktop.
Copying over data from Solid Cache to Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stats = { total: 0, success: 0, failed: 0 } | |
failed_entries = [] | |
redis = Redis.new(url: ENV[Rails.application.secrets.redis_cache_url]) | |
SolidCache::Entry.find_each(batch_size: 1000) do |item| | |
stats[:total] += 1 | |
key = item.key | |
# We need to use Rails.cache to access Solid Cache here. | |
# The reason is because SolidCache::Entry.first.value will | |
# be encoded in binary format and the data will be compressed. | |
# It is easier to get the data like so rather than reading | |
# directly from the table. | |
value = Rails.cache.read(key) | |
begin | |
if value != nil | |
redis.set(key, value) | |
stats[:success] += 1 | |
end | |
rescue => e | |
stats[:failed] += 1 | |
failed_entries << { | |
key: item.key, | |
error: e.message | |
} | |
puts "Failed to migrate entry with key: #{item.key}" | |
puts "Error: #{e.message}" | |
end | |
end | |
puts stats |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment