Created
March 17, 2014 14:24
-
-
Save nyilmaz/9600110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SearchResult<Integer> searchRelatedVideos(int videoId, int length) { | |
Cache<String, SearchResult<Integer>> cache = cacheFactory.getMemcachedMagnetCache("relatedvideo-cache"); | |
String key = "related/" + videoId + "/" + length; | |
SearchResult<Integer> result = cache.get(key); | |
if(result != null && result.getSize() != 0) { | |
logger.debug("Related videos found in cache for video : " + videoId); | |
return result; | |
} | |
VideoInfo video = videoInfoService.getVideoInfo(videoId); | |
StringBuilder builder = new StringBuilder(); | |
StringTokenizer tokenizer = new StringTokenizer(video.getTitle(), " \t\n\r()[]!:~*"); | |
while(tokenizer.hasMoreTokens()) { | |
builder.append(tokenizer.nextToken()).append(" "); | |
} | |
for(TinyTagInfo tag : video.getTagInfos()) { | |
if(tag == null) { | |
continue; | |
} | |
if(tag.getTagName() != null && !tag.getTagName().trim().equals("")) { | |
tokenizer = new StringTokenizer(tag.getTagName(), " \t\n\r()[]!:~*"); | |
while(tokenizer.hasMoreTokens()) { | |
builder.append(tokenizer.nextToken()).append(" "); | |
} | |
} | |
} | |
String query = builder.toString().replaceAll("\\?", ""); | |
query = StringUtils.removePunctuationCharacters(query, " "); | |
query = StringUtils.removeDuplicateWhitespaces(query, " "); | |
query = query.trim(); | |
try { | |
result = luceneService.searchVideos(query, null, ContentTypeCriterion.SEARCH_RESTRICTED.getContentType(), length + 1); | |
} catch(SearchException e) { | |
logger.error("Search exception occured while searching related videos : " + e.getMessage()); | |
return new SearchResultBean(new ArrayList<Integer>(0)); | |
} | |
result.getAllItems().remove(new Integer(videoId)); | |
if(result.getCachedSize() > length) | |
result.getAllItems().remove(result.getCachedSize() - 1); | |
cache.put(key, result); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment