Welcome Guest, Not a member yet? Register   Sign In
Like button in my codeigniter website
#1

I am creating a like button for my website so I need to know what is the best method?

Count vs Increment vs Count + Increment vs Cache

Please choose a scenario:

1. Incrementing a number


Pros

Easier and more performant to lookup a single number.
Easy to modify to skew results.

Cons

People can likely find a way to like multiple times.
Can't count individual likes if they need to be recalculated.
Can't remove likes for stuff like a user was deleted/banned.

2. Storing individual likes


Pros

Can be re-calculated if the numbers get skewed for some reason.
More verbose, you can literally count the likes yourself.
Can store identifying information with the likes to prevent multiple likes for same question or answer.
Can remove a vote if a user is deleted/banned (or just don't count votes from those users).
Cons
Less performant to count many rows, especially if the number gets really, really high (think [m|b|tr]illions+).
Uses more space. Likely not an issue, but when you have tons of rows, it might. Would need quite a lot though.

3. Cache query with $this->db->cache_on();


Pros

Speed and database will thank me

Cons

If people only view the the page would be ok because instead of select data from database would get from cache. Assuming that the like button is pressed every second on my website. So on that insert I must delete the cache from the server to make new one with the new count yes? So every second the cache will be deleted and replaced with new one. Would be this ok? This is my function to create a like.

PHP Code:
function create($data) {
$this->db->cache_delete('stream''question');
$this->db->cache_delete('stream''category');
$this->db->insert($this->_table$data);
return 
$this->db->insert_id();

Reply
#2

Even in the best-case (or worst-case, depending on your point of view) scenario, the page will get more reads than "likes", and, eventually, the majority of the people that will "like" the page will have done so. This definitely works in favor of the cache method.

If you're really concerned about excessive deletes in the cache, you could perform the write, but require a minimum age on the existing cache before it gets deleted. This would mean that the numbers might jump, especially under heavy load, and you would have to rely on JavaScript for people to see their "likes" counted immediately, but you wouldn't be constantly deleting the cache file and rebuilding it under heavy load. Plus, once things slow down, most of the "likes" would be immediately counted, since the cache file would be less likely to be fresh.

If you're concerned that a "like" may not be reflected on the page because of something odd (like two "like"s in a short time period with no activity afterwards), you could always have the cache rebuilt or deleted automatically on a schedule. You could even get more accurate by tracking when the relevant data was last changed and comparing that to when the cache file was built to determine whether the cache might be stale.
Reply
#3

(This post was last modified: 08-20-2015, 03:42 PM by sebastianvirlan.)

The main problem I see is that:

in my website I have this url: http://website.com/stream/category , stream controller and category method.

On this category method I have parameters like:

http://website.com/stream/category/art_and_culture
http://website.com/stream/category/busin..._financial
http://website.com/stream/category/auto_and_moto
http://website.com/stream/category/beauty_and_fashion

and a lot moore.

Ok everything on this page saves in cache in stream+category folder ok?

Imagine that I have 20 categories, each one with 50 pages of 10 questions on each page 

AND ON A SINGLE PRESS OF A LIKE BUTTON EVERYTHING FROM THIS FOLDER stream+category GET DELETED !!!

I think after the like and after deleted that cache the cache for all pages from stream+category will not be builded totally because another like will come and so ...

What can you say about this?

You told me 
Quote:If you're really concerned about excessive deletes in the cache, you could perform the write, but require a minimum age on the existing cache before it gets deleted.

But this would work if in a second 5 users press a like? I think all will mess up .
Instead of deleting all from stream+category folder can I could just delete the file from the folder responsable with the data?
Here is a picture with 2 example of questions:
[Image: 1DefNdW.png]
Reply
#4

(08-20-2015, 03:34 PM)sebastianvirlan Wrote: On this category method I have parameters like:
http://website.com/stream/category/art_and_culture
http://website.com/stream/category/busin..._financial
http://website.com/stream/category/auto_and_moto
http://website.com/stream/category/beauty_and_fashion

Ok everything on this page saves in cache in stream+category folder ok?
Imagine that I have 20 categories, each one with 50 pages of 10 questions on each page 
AND ON A SINGLE PRESS OF A LIKE BUTTON EVERYTHING FROM THIS FOLDER stream+category GET DELETED !!!
I think after the like and after deleted that cache the cache for all pages from stream+category will not be builded totally because another like will come and so ...

What can you say about this?

You told me 


Quote:If you're really concerned about excessive deletes in the cache, you could perform the write, but require a minimum age on the existing cache before it gets deleted.

But this would work if in a second 5 users press a like? I think all will mess up .
Instead of deleting all from stream+category folder can I could just delete the file from the folder responsable with the data?

This is why code should be optimized based on your requirements and analysis of the application. If it doesn't make sense to use database caching for this purpose on these pages, then don't use it. You didn't include this information in your original question. Database caching isn't your only option for caching the data, either. You could use the cache library to store the number of likes separately from the database cache that handles the rest of the data on the page(s).

In the worst case, you could probably figure out a method of deleting the correct file for the query if you have to use database caching, but it's not something supported by CI directly.

My point with the minimum age on the cache was this: if you write to the database but only cache the value in the database when the cache is stale, the value in the database will be correct but the value in the cache may be incorrect for a short amount of time. To make sure the cached value doesn't corrupt the database value in the update, you use something like this in your update SQL:
Code:
UPDATE table_likes SET column_likes = column_likes + 1 where id = 2

So, instead of adding one to the cached value and setting that value in an update statement, you tell the database to set the column's value to the current value + 1, and you still don't have to query the database to get the value.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB