-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved performance #5
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- 'id' param will help improving the overall performance of application as the user will be able to fetch the required data directly by passing the 'id' i.e. previously used to store the data. - new TCs written for modified methods added keyword arguments for 'get' methods added 'keyword arguments' & 'highredis' functionality
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,35 @@ | |
require 'json' | ||
require 'time' | ||
require 'redis' | ||
require 'redis/connection/hiredis' | ||
|
||
module Ariadne | ||
DataUtil.init_redis_cli(Redis.new(url: ENV['REDIS_URL'])) | ||
def self.get_app_name(app_name: nil) | ||
app_name ||= ENV['APP_NAME'] | ||
app_name || '' | ||
end | ||
|
||
DataUtil.init_redis_cli(redis_obj: Redis.new(url: ENV['REDIS_URL'])) | ||
|
||
def self.insert_data(options = {}) | ||
def self.insert_data(options: {}, app_name: nil) | ||
options[:id] ||= options['id'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this line for? |
||
options[:app_name] ||= options['app_name'] | ||
raise 'Please specify data to be inserted for Ariadne.insert_data method!' if options.size <= 0 | ||
raise 'Please specify id to be passed for Ariadne.insert_data method!' if options[:id].nil? || options[:id].size <= 0 | ||
DataUtil.insert_data_in_redis(options.merge!(app_name: get_app_name(options[:app_name]))) | ||
raise 'Please specify data to be inserted for Ariadne.insert_data method!' if options.empty? | ||
raise 'Please specify id to be passed for Ariadne.insert_data method!' if options[:id].nil? || options[:id].size == 0 | ||
DataUtil.insert_data_in_redis(options: options, app_name: get_app_name(app_name: app_name)) | ||
rescue StandardError => e | ||
puts e | ||
e | ||
end | ||
|
||
def self.get_data(id = nil, app_name = '') | ||
DataUtil.get_data_from_redis(id, get_app_name(app_name)) | ||
def self.get_data(id: nil, app_name: nil) | ||
DataUtil.get_data_from_redis(id: id, app_name: get_app_name(app_name: app_name)) | ||
rescue StandardError => e | ||
puts e | ||
e | ||
end | ||
|
||
def self.get_data_with_time_difference(id = nil, app_name = '') | ||
redis_data = get_data(id, get_app_name(app_name)) | ||
def self.get_data_with_time_difference(id: nil, app_name: nil) | ||
redis_data = get_data(id: id, app_name: app_name) | ||
default_time_diff_threshold = 30 | ||
output_data = [] | ||
unless redis_data.nil? | ||
|
@@ -42,9 +47,4 @@ def self.get_data_with_time_difference(id = nil, app_name = '') | |
end | ||
output_data.to_json | ||
end | ||
|
||
def self.get_app_name(app_name = '') | ||
app_name = ENV['APP_NAME'] if app_name.nil? || app_name.size <= 0 | ||
app_name || '' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
|
||
module DataUtil | ||
def self.init_redis_cli(obj = nil) | ||
@redis_cli = obj | ||
def self.init_redis_cli(redis_obj: nil) | ||
@redis_cli = redis_obj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this not be in a begin-rescue-end block? given that |
||
end | ||
|
||
def self.get_data_from_redis(id, app_name) | ||
def self.get_data_from_redis(id:, app_name:) | ||
key = id.nil? ? "#{app_name}*" : "#{app_name}:#{id}" | ||
keys = @redis_cli.keys key | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure why I dont like the way this has been written The caller of this method already knows if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is not possible for the caller to know the key! |
||
raise "Data not available for #{app_name}!" if keys.empty? | ||
|
@@ -15,8 +15,8 @@ def self.get_data_from_redis(id, app_name) | |
puts e | ||
end | ||
|
||
def self.insert_data_in_redis(options = {}) | ||
key = "#{options[:app_name]}:#{options[:id]}" | ||
def self.insert_data_in_redis(options:, app_name:) | ||
key = "#{app_name}:#{options[:id]}" | ||
options[:time] = Time.now | ||
redis_data = nil | ||
@redis_cli.pipelined do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require_relative 'data_util' | ||
require 'fakeredis' | ||
|
||
DataUtil.init_redis_cli(Redis.new) | ||
DataUtil.init_redis_cli(redis_obj: Redis.new) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the above two lines can be written like this