Ads 468x60px

Friday, June 22, 2018

How to use Redis for caching data in Rails

Why Redis is used?

This is a very basic question and it comes because you can do what redis does with postgresql for the purpose of solving the functionality aspect. What redis does is, it stores the key value pair and most of the operations can be executed in O(N) time only (for more information for every command time complexity check the redis documentation). Thus caching enables retrieval of data at much higher speeds and is optimal for tasks such as pub/sub and queries that need quicker access. 

Redis Configuration 

Initialization - redis/config/ini_redis.rb
redis_host = Rails.application.secrets.redis && Rails.application.secrets.redis['host'] || 'localhost'
redis_port = Rails.application.secrets.redis && Rails.application.secrets.redis['port'] || 6379

# The constant below will represent ONE connection, present globally in models, controllers, views etc for the instance. No need to do everytime
REDIS = redis_host, port: redis_port.to_i)

model.rb file - models/user.rb

def online?
You can use views such as like this, views/users/show.html.erb
@users_ol = User.where(:id => @id).select(&:online?)

Monday, May 28, 2018

CodeChef Workshop Experience

A little self bragging (About the Author)

I have been involved in and enjoyed building projects since a very long time. As of now I am doing my integrated msc mathematics from NIT Surat (aka SVNIT). I have had very little experience with competitive coding problems and the workshop had a lot of new things in store for me. This workshop taught us concepts with problem solving approach which I enjoyed thoroughly. My main aim to attend this workshop was to improve my coding skills and be a more productive/efficient developer than before. 
I heard about this workshop through the codechef website and was one of the early bird registratant to it. The workshop was well timed and started 4 days after my end-semester exams, so I packed up my bags left for Hyderabad. 


The workshop was conducted at MLRIT (stands for something something Institute of Technology). The campus was not widespread or huge but had some really amazing facilities. It had olympic size indoor badminton stadium, a well mantained cricket ground, a flourshing incubation cell for startups and some pretty amazing eating outlets. The hostels' hygine was not upto the mark and apparently the institute took good care of its' mosquitoes and insects as well as the students in it. The quality of food was pretty good considering it was mess food. Although a little variety for students coming from north would have been more apprciated. (Yes. I come from north and I like eating chapatti and using spoons for god's sake!)

I arrived on 14th May to MLRIT and the workshop kicked off from 15th May. 


The main objective of the workshop was to incultivate problem solving skills using data strucutres and algorithms. It was a beginner level workshop which covered, Greedy Algorithms, Arrays, Stacks, Queues, Dynamic Programming. We had 7-12 questions discussed on a daily basis, out of which 4-5 were compulsary problems whereas rest were optional. The questions were from codechef website.  As the workshop was targetted at people who wanted to be introduced to programming the questions were kept fairly simple and basic. (and rightly so!). 


The attendees of the workshop was a pretty good mix of people. We had couple of school students and a few professionals but most of the crowd was college students. My team of 8 also had pretty high diversity and included a pro coder, a born management guy, some sporty folks, some sincere folks and some seriously lazy people. To everyone's suprise (and our's also) we ended up coming third as a team in the complete workshop. 
The lectures were conducted by Arjun Arul (google his resume, it's pretty dope) and he made sure that we understood what he was saying. He very quickly understood the wrong approaches as well as the correct ones and took us on lullaby ride of a wrong approach just to prove it is wrong and how to arrive at the right one. We also had trainers that helped us during the lab sessions. All of them were 5-star rated codechef members and were really helpful and got us through from understanding the logic to debugging our code!

Stuff I need to mention

We also had a lot of games. This included badminton, table tennis, volleyball, dodgeball, chess, scrabble, othello and football. Our team went to the semi-finals of Volleyball, Chess and Scrabble. The points from the games were added to our team score on which the top 3 teams were declared. Overall the workshop served as complete package which had sports, learning, coding and a lot of fun. 


The people were easy to make friends with(including trainers, and lecturer) and the environement was very receptive of the differences of ideologies and intellectual capacties. It gave me good grasp on basic concepts and broke the inertia which prevented me from participating and being involved in coding contests. I plan to carry forward the learning by myself now and see where it leads me. 

P.S Will update this posts with some pics as soon as I receive them. 

Tuesday, February 27, 2018

Mid Semester Exams are arriving

In one of the leading universities across India, there has been a massive revolution. The students of SVNIT (Seriously Vella National Institute of TimePass) have started to laugh at the professor's jokes. When we reached out to the students and asked them why this sudden degradation in sense of humour, the students were just as clueless as if they have been asked about the topics that they are studying. "Students are just reacting to the stress, this is temporary and would soon go away" we overheard one watchman, talking to another. One of the students named Shashank has already started with the syllabus, when the spokesperson reached out to him he denied all the allegations and vehemently accused our great education system to show his loyalty for the majority. He then quickly disappeared into the abyss of library. Another student Rakesh has also picked up a book, .. no no not the subject book  but the book "Exam Warriors", by our honorable Prime Minister which talks about how to reduce stress among students. After reading the book, Rakesh has decided to increase his sleeping quota from 14 hours a day to 16 hours a day as there is a complete chapter in the book titled, "Sleep is your greatest weapon, embrace it". Rakesh also claims that he has been supporting Prime Minister's campaign of "Exam pe charcha" since last year but the faculty here had him detained for the same. He says "Exam pe charcha is one of the best ways to reduce stress during exams as most students study just a night before exams" but for some reason the authorities have not allowed discussions during the exam as of time of writing this article. Some of the students have even reduced meme tagging their friends from 4 a day to just 2 a day. Inspired by this sudden shift in student's behaviour the UN has amended it's Article 3 of the Geneva Convention which now states that all prisoners must be treated humanely, and not tortured with techniques like induced psychosis, physical torture and one night exam cramming.

Tuesday, February 20, 2018

Amazon Slay - Available only for prime members.

Amazon has been creating disruptive innovations to get into our mobile phones, bedrooms and now they have hit the highroad with the launch of their new service - Amazon Slay. Amazon Slay provides one day killing service for prime members where as normal users would have to wait longer. It also integrates well with other amazon services such as kindle, echo and bundles with developer ready API. One of the spokesperson with the knowledge of this subject told us that "buyers can choose from hanging, gun or getting beaten to death while ordering the service, as compared to just gunshot offered by their competitors". This was clearly a reference to the service Flipkart Bhai which was started a month ago but has faced a very stiff competition from global players. We tried reaching out to people from Flipkart Bhai but they declined to comment on the issue. Amazon claims that they have managed to improve suicide attempts by whooping 11% and are a one stop service for anyone planning any type of homicide.  Not only this amazon offers real time notifications of the killer and gives an option to kill the neighbour in case the person in question is not available. This service is currently available to prime members only and would be expanded soon given the unprecedented demand.  

My experiments on Optimizing Profits in CryptoCurrencies.

Sometimes you just know that something won't work and yet somehow you need to watch it fail just to believe that it won't. Some people may call it a foolhardy attempt but this is the kind of incredulity of men for new ideas until they had good amount of exposure of things often brings disruptive innovation.

CryptoCurrency is the new buzzword around town and everyone wants to get their hands dirty.  I decided to give it a shot myself. The idea was simple, get the live feed of cryptocurrency (using binance API) and somehow optimize the algorithm for buying and selling to make maximise profits. Fueled by this idea, I signed up on binance (it is an exchange for trading cryptocurrencies) and got onboard their api platform.

Approach #1.
I begum hitting the API at an interval of 0.5 seconds and recorded the highs and lows. The point there was a change of slope, I decided to create sandbox transaction. If the slope changes form positive to negative, it is a good time to sell because the probability that it is going to keep following the decrement pattern are high. Similarly