• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Assignment  RSS feed

 
Jacka sharmaa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Challenge:
Using Java or C# write a simple cache library. Then use that library to
implement the following interface (in Java, feel free to convert to C#).
public interface LoginService {
/**
* Checks whether the user has logged in within the last 24 hours
* @param userId
* @return
* True if the user HAS logged in within 24 hours
* False otherwise
*/
public boolean hasUserLoggedInWithin24(String userId);
/**
* Sets the last login time for the user to now.
* @param userId
*/
public void userJustLoggedIn(String userId);
}
To simplify things, you can use the following library to pretend read and
write to the database.
public class FakeDBAccess {
public Date getLastLoginForUser(String userId) {
if(Math.random() < .5) return new Date(System.currentTimeMillis());
return new Date(System.currentTimeMillis()-42*60*60*1000);
}
public void setLastLoginForUser(String userId, Date date) throws DBEx {
// do nothing
}
}
Important details to consider:
1. There are more users than can fit in the cache.
2. There are far more calls to the service than users in the database.
3. There are other programs that update the database other than this one.
3a. This means the cache can go stale!
3b. When does staleness matter?
3c. You have no control over the other programs.
--------------------------------------------------------------------------------------------------------------
Questions:
1. What was the reasoning behind your implementation of the cache?
2. How does your cache improve performance?
3. What are the various usage patterns that make the cache more or less
effective in terms of performance?
Discuss why the cache will be more effective under some scenarios and less
effective under others. For example if hasUserLoggedInWithin24 is called 80
times in a row for the same person, the cache is very effective. When is it
less effective?
If you have questions please feel free to ask them



 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

That looks like homework that you should be doing in order to learn something. Nobody here will do it for you, but folks will help you do it yourself. Do you have a question about any part of it?
 
Jacka sharmaa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
little confusion.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't understand what you're being asked, then I think you should get clarification from your teacher or teaching assistant. Presumably he thinks you should be equipped with the necessary knowledge to tackle this problem.

To start with, are you clear on the concept what a cache is, why you might want to use one, and what is meant by "stale" in this context? If not, everything else won't make sense.
 
Jacka sharmaa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already implemented database methods.
setLastLoginForUser(userid)
hasuserloggedinwithin24hour(userid)

Am I right ?
------------------------------------------------------------

public boolean hasUserLoggedInWithin24(String userId){

If user exist in cache data structure return from there - return ( true/false)

else call method hasuserloggedinwithin24hour in database return (true/false)
and update cache data struture so it can be used next time.

}


public void userJustLoggedIn(String userId){

set in local datastructure

call method setLastLoginForUser(userid, new Date());

}

------------------------------------------------------------
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't understand what the methods should do, or how they fit into the bigger scheme of things. Before you write any code you should write down how your algorithms will work; if you post the description we may be able to comment.
 
Jacka sharmaa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!