Skip to content

Commit

Permalink
Merge pull request #53 from nastiausenko/feature/original-link-redirect
Browse files Browse the repository at this point in the history
Update Javadoc for link cache and redirect controller
  • Loading branch information
IvanShalaev1990 authored Apr 19, 2024
2 parents b0e7849 + e5783d2 commit 0940382
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,47 @@
import java.util.HashMap;
import java.util.Map;

/**
* Component class for caching links to optimize redirection performance.
*
* <p>This class provides a simple in-memory cache for storing links by their short links.
* It allows for quick retrieval of links based on short links, reducing the need for repeated
* database queries during redirection requests.
*
* @author Egor Sivenko
*/
@Component
@RequiredArgsConstructor
public class LinkCache {

private final Map<String, Link> cache = new HashMap<>();

/**
* Checks if the cache contains the specified short link.
*
* @param shortLink the short link to check for in the cache
* @return true if the cache contains the short link, false otherwise
*/
public boolean containsShortLink(String shortLink) {
return cache.containsKey(shortLink);
}

/**
* Retrieves the link associated with the specified short link from the cache.
*
* @param shortLink the short link to retrieve the associated link for
* @return the link associated with the short link, or null if not found in the cache
*/
public Link getByShortLink(String shortLink) {
return cache.get(shortLink);
}

/**
* Puts a new link into the cache with the specified short link.
*
* @param shortLink the short link to use as the cache key
* @param link the link to be cached
*/
public void putLink(String shortLink, Link link) {
cache.put(shortLink, link);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
* Controller class for handling link redirection requests.
*
* <p>This class provides an endpoint for redirecting short links to their corresponding long links.
* When a request is made with a short link, the controller fetches the corresponding long link
* from the database using the {@link LinkService#findByShortLink(String)}, updates link statistics,
* and redirects the user to the long link.
* Additionally, it updates the link's expiration time to extend its validity.
* It first checks if the short link is cached in {@link LinkCache}. If the short link is found in the cache,
* it retrieves the link directly from the cache; otherwise, it queries the {@link LinkService} to fetch
* the link from the database. After retrieving the link, it updates its statistics and expiration time,
* caches the link for future requests and redirects the user to the corresponding long link.
*
* @author Egor Sivenko
* @see org.springframework.web.servlet.view.RedirectView
Expand All @@ -27,12 +27,8 @@
@RequiredArgsConstructor
public class LinkRedirectController {

/**
* The service responsible for managing links
*/
private final LinkService linkService;

private final LinkCache linkCache;
private final LinkService linkService;

/**
* Redirects a request with a short link to its corresponding long link.
Expand All @@ -50,14 +46,25 @@ public RedirectView redirectToOriginalLink(@PathVariable("shortLink") String sho
return redirectToLongLink(link);
}

/**
* Updates the link statistics, expiration time, and caches the link.
*
* @param link the link to be updated
*/
private void updateLinkStats(Link link) {
link.setStatistics(link.getStatistics() + 1);
link.setExpirationTime(LocalDateTime.now().plusMonths(1));

linkService.update(link);
linkService.save(link);
linkCache.putLink(link.getShortLink(), link);
}

/**
* Redirects to the long link.
*
* @param link the link to redirect to
* @return a RedirectView object directing the user to the long link
*/
private RedirectView redirectToLongLink(Link link) {
RedirectView redirectView = new RedirectView();
redirectView.setUrl(link.getLongLink());
Expand Down

0 comments on commit 0940382

Please sign in to comment.