How do I store this in Redis? - database

I have many products (product_id). Users (user_id) view the products.
I want to query which users viewed whatever product in the last 24 hours. (In other words, I want to keep a list of user_ids attached to that product_id...and when 24 hours is up for a user, that user pops off that list and the record disappears)
How do I store this in Redis? Can someone give me a high-level schema because I'm new in Redis.

For something similar I use a sorted set with values being user ids and score being the current time. When updating the set, remove older items with ZREMRANGEBYSCORE as well as updating the time score for the current user.
Update with code:
Whenever a new item is added:
ZREMRANGEBYSCORE recentitems 0 [DateTime.Now.AddMinutes(-10).Ticks]
ZADD recentitems [DateTime.Now.Ticks] [item.id]
To get the ids of items added in the last 10 minutes:
ZREVRANGEBYSCORE recentitems [DateTime.Now.Ticks] [DateTime.Now.AddMinutes(-10).Ticks]
Note that you could also use
ZREVRANGE recentitems 0 -1
if you don't mind that the set could include older items if nothing has been added recently.
That gets you a list of item ids. You then use GET/MGET/HGET/HMGET as appropriate to retrieve the actual items for display.

If you want redis keys to drop off automatically then you'll probably want to use a redis key for every user_id-to-product_id map. So, you would write by doing something like redis.set "user-to-products:user_id:product_id", timestamp followed by redis.expire "user-to-products:user_id:product_id" 86400 (24hrs, in seconds).
To retrieve the current list you should be able to do redis.keys "user-to-products:user_id:*"

Related

Show ALL items in order in Mongodb database

For some reason when I run db.products.find().pretty(), it doesn't list all the items in my database, and the ones it does list are not in order. Any idea why or how to list everything? It does give me the option to run 'it' after to show more, but it still doesn't show them all or in order. I just want to see all 100 products in order and pretty().
I can understand it not being on order, of productId, because I may not know to do so, but at least can I get it to list everything??
for setting order you can use sort().
db.sortData.find().sort({id:-1}).pretty()
here, -1 = Descending Order and 1 = Ascending Order on id field of collection.
By Default, mongo shell batch size returns 20 records at a time, then show more have to enter, if you want to changes size you can fire this command.
DBQuery.shellBatchSize = 30
so, now 30 records of collection mongo shell returns rather than 20.

Get value of unknown keys

This is my firebase database structure in the image :
I'll explain the structure aswell,
I have a forum, which people can post trades in.
Every post has some random user key as you can see in the picture.
Also every post has a list of items which the user wants to sell or buy ('have' or 'want', in the image).
the number of items in each trade can change from 1 to 10.
I want to fetch all of the forum posts of the users that are selling ( or buying ) with some item id.
For example : Get forum posts of users selling items with 'Id' of 'Some Item Name'
How do I do this? I can't seem to get reference to each item in the inventory
since you can't do multiple orderByChild.
If you think it's impossible with this DB structure, Please offer me an alternative.
I want to know whether I'm wasting my time with firebase and this is impossible to do :(
Please note that I have a lot of data in my DB so I can't just filter the posts on the client side.
Either you can change your database structure OR You can create a different node which can contain the metadata of all the "have" OR "want" items with the itemID, userID and "have" or "want" node child number(in this case it should be 0-9, as there are 10 items in each type). So whenever you are saving/deleting you data from "have" or "want" section you have to do the same operation in the other new metadata table also.
You can run your query on this node to get the desired item and then with the help of result data you get those particular items directly by creating a reference at runtime as you are having userId, have or want type, itemId.
your metadata should be something like.
metadata
|
|
+{randonId1}
|
|-type : "have" or "want"
|-userId : "randonId of user".(Kt0QclmY3.as shown in picture)
|-Id: "Breakout Type-S"
|-childOnNode: 0, (0-9)
+{randonId2}
|
|-type : "have" or "want"
|-userId : "randonId of user".(Kt0R48Cp..as shown in picture)
|-Id: "Breakout"
|-childOnNode: 0, (0-9)

Paging with a data set that can be changing?

I'm sure there is something out there about this topic but I just can't figure out how to word a search for it.
I have a table of records that gets loaded into a paging grid in the UI. The user has the ability to update/modify these records..also multiple users can use the system at once all hitting the same data. I have a filter on the paging grid allowing the user to see only X type of records.
When the user first enters with filter X selected they see items 1-25 on page 1 of 2. They page to the second page where the items should be 26-50..but before they paged lets say 25 records on the first page had their type changed by another user, now they don't appear when selecting that filter. So now we have 25 less items to page through which means items that were 26-50 before are now items 1-25 and what was page 2 is now page 1 and there is no page 2...
You can probably see the issue I'm getting into, I'm passing an offset to the query to get the next page of results..but now that offset is so high it returns a blank page of records confusing the user and our record processing.
There isn't really an easy solution to this problem. Even GMail/Google doesn't show the exact number of messages/pages found when searching something.
The first thing you can do (if you use a DataGrid/CellTable) is set the boolean exact as false when you call updateRowCount, and give it your current number of records instead of your total number of records. This will make the pager display "1 - 25 of over 25" instead of "1 - 25 of 50".
The next possibility is to update the row count regularly (using RPC polling to check for new/deleted records - or using server push techniques, see GWTEventService and ServerPushFAQ).
You can also check if your request returns items or not, and cancel the call/update the row count if it doesn't.

managing View Count of webpage

I am creating a jsp page where a single news item is shown, just like the single question on this StackOverflow page. I need to store the view count in a database.
I can simply increment the view count in the database this way:
`UPDATE table SET views=views+1 WHERE newsId=4458;`
But what if a single user hits that page again and again and again, say 50 times... In this case, my view count will not represent unique visitors to the site. How can I avoid this?
I want something like StackOverflow's view count system.
well one solution could be you make a new table (lets say its called views) with the following columns 'viewid' 'newsid' 'ipaddress' (obviously newsid is the foreign key)
every time someone visits ur website u check if his ip is already in that table for that news item.
Select count(*) AS count FROM views WHERE newsid=1234 AND ipaddress=123.456.125
if count equals 0 you insert like this
INSERT INTO views('newsid', 'ipaddress') VALUES(1234, 123.146.125)
Now when you want to fetch the viewcount:
Select count(*) AS count FROM views WHERE newsid=1234
you can enhance this by adding another column called 'count' and 'lastviewed' (type datetime) to the table views.
Lets say you allow every ip to be counted another time if the day doesnt match.
This way you will get a pretty accurate viewcount :)
to fetch the new viewcount you can simply use SUM
Select SUM(count) AS count WHERE newsid=1234
Good luck :)
You may want to reference spencer7593's answer here, where he basically suggests having both (1) a view count column, for quickly generating your webpages and (2) a separate views table, which records the user id and timestamp pertaining to each view, for the sake of analytics.
I would suggest that this views table could also be used to query if a viewing has occurred for a particular user with a particular piece of content in the last day (or week) when determining if the view count column should be incremented. By checking if a view of a certain item has occurred only in the last day (or week), the DB queries should be substantially less expensive while still protecting the integrity of your view count.
i say use a counter in Javscript . Increment the variable on body onclick. eg:
<script language=”javascript”>
function pageCounter()
{
var counter = 0;
counter = counter++;
}
</script>
<body onclick = “pageCounter()”>
and you can update the value with onclose ..

Magento - get list of items from orders for specific date range

Magento database name convention is not trivial. How to get these fields below for last 7 days?
Last Name
First Name
Address
City
State
Zip
Phone
Email
Amount
Order #
Item #
I could not tell if you were looking for some PHP/Magento code or if you are looking to access the database directly. It might be "better" to create yourself a custom module that fetches this info using the Magento/Zend framework, but since I don't know the code off the top of my head I'll redirect you to the following link which has a very nice SQL query that will pull that info for you (and more).
http://www.magentocommerce.com/wiki/groups/207/fedex_-_shipping_view
You probably just need to add something like this to the end to filter the last 7 days
where so.created_at > NOW() - INTERVAL 7 DAY

Resources