PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/spring/forum/services/MemcachedService.java

https://github.com/gRanit-/spring-forum
Java | 274 lines | 230 code | 43 blank | 1 comment | 46 complexity | f08dedfd50764f9afef057003a21759c MD5 | raw file
  1. package spring.forum.services;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Set;
  5. import java.util.TreeSet;
  6. import net.spy.memcached.MemcachedClient;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import spring.forum.models.Post;
  10. import spring.forum.models.Topic;
  11. import spring.forum.utils.CollectionUtility;
  12. import spring.forum.utils.CollectionUtility.*;
  13. @Service
  14. public class MemcachedService {
  15. //@Autowired
  16. private MemcachedClient memcachedClient;
  17. public void addPost(Post post) {
  18. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  19. .get("posts_1");
  20. int i = 1;
  21. if (set == null) {
  22. set = new TreeSet<Element>(new ComparatorUtil());
  23. } else
  24. while (set.size() > 1000) {
  25. i++;
  26. set = (Set<CollectionUtility.Element>) memcachedClient
  27. .get("posts_" + i);
  28. if (set == null)
  29. set = new TreeSet<Element>(new ComparatorUtil());
  30. }
  31. set.add(new CollectionUtility.Element((post.getAuthor().getId()), post
  32. .getId(), post.getTopic().getId()));
  33. memcachedClient.delete("posts_" + i);
  34. memcachedClient.add("posts_" + i, 86400, set);
  35. String postString = "p_" + post.getId();
  36. memcachedClient.add(postString, 86400, post);
  37. }
  38. public void addTopic(Topic topic) {
  39. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  40. .get("topics_1");
  41. int i = 1;
  42. if (set == null) {
  43. set = new TreeSet<Element>(new ComparatorUtil());
  44. } else
  45. while (set.size() > 1000) {
  46. i++;
  47. set = (Set<CollectionUtility.Element>) memcachedClient
  48. .get("topics_" + i);
  49. if (set == null)
  50. set = new TreeSet<Element>(new ComparatorUtil());
  51. }
  52. set.add(new Element((topic.getAuthor().getId()), (long) -1, topic
  53. .getId()));
  54. memcachedClient.delete("topics_" + i);
  55. memcachedClient.add("topics_" + i, 86400, set);
  56. String topicString = "t_" + topic.getId();
  57. memcachedClient.add(topicString, 86400, topic);
  58. }
  59. public void updatePost(Post post) {
  60. long topicID = post.getTopic().getId();
  61. long postID = post.getId();
  62. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  63. .get("posts_1");
  64. Element element = new Element(post.getAuthor().getId(), postID, topicID);
  65. int i = 1;
  66. while (!set.remove(element)) {
  67. i++;
  68. set = (Set<CollectionUtility.Element>) memcachedClient.get("posts_"
  69. + i);
  70. }
  71. set.add(element);
  72. memcachedClient.delete("posts_" + i);
  73. memcachedClient.add("posts_" + i, 86400, set);
  74. String postString = "p_" + post.getId();
  75. memcachedClient.delete(postString);
  76. memcachedClient.add(postString, 86400, post);
  77. }
  78. public void updateTopic(Topic topic) {
  79. long topicID = topic.getId();
  80. long userID = topic.getAuthor().getId();
  81. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  82. .get("topics_1");
  83. Element element = new Element(topic.getAuthor().getId(), (long) -1,
  84. topicID);
  85. int i = 1;
  86. while (!set.remove(element)) {
  87. i++;
  88. set = (Set<CollectionUtility.Element>) memcachedClient
  89. .get("topics_" + i);
  90. }
  91. set.add(element);
  92. memcachedClient.delete("topics_" + i);
  93. memcachedClient.add("topics_" + i, 86400, set);
  94. String topicString = "t_" + topic.getId();
  95. memcachedClient.delete(topicString);
  96. memcachedClient.add(topicString, 86400, topic);
  97. }
  98. public void deletePost(Post post) {
  99. long topicID = post.getTopic().getId();
  100. long postID = post.getId();
  101. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  102. .get("posts_1");
  103. Element element = new Element(post.getAuthor().getId(), postID, topicID);
  104. int i = 1;
  105. while (!set.remove(element)) {
  106. i++;
  107. set = (Set<CollectionUtility.Element>) memcachedClient.get("posts_"
  108. + i);
  109. }
  110. String postString = "p_" + post.getAuthor().getId() + "_"
  111. + post.getTopic().getId() + "_" + post.getId();
  112. memcachedClient.delete(postString);
  113. }
  114. public void deleteTopic(Topic topic) {
  115. long topicID = topic.getId();
  116. long userID = topic.getAuthor().getId();
  117. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  118. .get("topics_1");
  119. Element element = new Element(topic.getAuthor().getId(), (long) -1,
  120. topicID);
  121. int i = 1;
  122. while (!set.remove(element)) {
  123. i++;
  124. set = (Set<CollectionUtility.Element>) memcachedClient
  125. .get("topics_" + i);
  126. }
  127. String topicString = "t_" + topic.getId();
  128. memcachedClient.delete(topicString);
  129. }
  130. public Post getPost(long postID) {
  131. String postString = "p_" + postID;
  132. return (Post) memcachedClient.get(postString);
  133. }
  134. public Topic getTopic(long topicID) {
  135. return (Topic) memcachedClient.get("t_" + topicID);
  136. }
  137. public List<Post> getAllPosts() {
  138. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  139. .get("posts_1");
  140. List<Post> postList = new ArrayList<Post>();
  141. int i = 1;
  142. if (set != null)
  143. while (set.size() > 0) {
  144. i++;
  145. for (Element e : set) {
  146. long postID = e.postID;
  147. String postString = "p_" + postID;
  148. Post post = (Post) memcachedClient.get(postString);
  149. if (post != null)
  150. postList.add(post);
  151. }
  152. set = (Set<CollectionUtility.Element>) memcachedClient
  153. .get("posts_" + i);
  154. if (set == null)
  155. break;
  156. }
  157. return postList;
  158. }
  159. public List<Topic> getAllTopics() {
  160. Set<CollectionUtility.Element> set = (Set<CollectionUtility.Element>) memcachedClient
  161. .get("topics_1");
  162. List<Topic> topicsList = new ArrayList<Topic>();
  163. int i = 1;
  164. if (set != null)
  165. while (set.size() > 0) {
  166. i++;
  167. for (Element e : set) {
  168. long topicID = e.topicID;
  169. String topicString = "t_" + topicID;
  170. Topic topic = (Topic) memcachedClient.get(topicString);
  171. if (topic != null)
  172. topicsList.add(topic);
  173. }
  174. set = (Set<CollectionUtility.Element>) memcachedClient
  175. .get("topics_" + i);
  176. if (set == null)
  177. break;
  178. }
  179. return topicsList;
  180. }
  181. public List<Post> getPostsByTopic(long topicID) {
  182. TreeSet<CollectionUtility.Element> set = (TreeSet<CollectionUtility.Element>) memcachedClient
  183. .get("posts_1");
  184. List<Post> postList = new ArrayList<Post>();
  185. int i = 1;
  186. if (set != null)
  187. while (set.size() > 0) {
  188. i++;
  189. Set<CollectionUtility.Element> subset = set.subSet(new Element(
  190. (long) 0, (long) 0, topicID), new Element((long) 0,
  191. (long) 0, topicID + 1));
  192. for (Element e : subset) {
  193. long postID = e.postID;
  194. String postString = "p_" + postID;
  195. Post post = (Post) memcachedClient.get(postString);
  196. if (post != null)
  197. postList.add(post);
  198. }
  199. set = (TreeSet<CollectionUtility.Element>) memcachedClient
  200. .get("posts_" + i);
  201. if (set == null)
  202. break;
  203. }
  204. return postList;
  205. }
  206. public List<Topic> getTopicsByUser(long userID) {
  207. TreeSet<CollectionUtility.Element> set = (TreeSet<CollectionUtility.Element>) memcachedClient
  208. .get("topics_1");
  209. List<Topic> topicsList = new ArrayList<Topic>();
  210. int i = 1;
  211. if (set != null)
  212. while (set.size() > 0) {
  213. i++;
  214. Set<CollectionUtility.Element> subset = set.subSet(new Element(
  215. userID, (long) -1, (long) 0), new Element(userID + 1,
  216. (long) -1, (long) 0));
  217. for (Element e : set) {
  218. long topicID = e.topicID;
  219. String topicString = "t_" + topicID;
  220. Topic topic = (Topic) memcachedClient.get(topicString);
  221. if (topic != null)
  222. topicsList.add(topic);
  223. }
  224. set = (TreeSet<CollectionUtility.Element>) memcachedClient
  225. .get("topics_" + i);
  226. if (set == null)
  227. break;
  228. }
  229. return topicsList;
  230. }
  231. }