Package com.tubeilike.util

Source Code of com.tubeilike.util.JavaCacheHandle

package com.tubeilike.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.google.appengine.api.datastore.Text;
import com.tubeilike.entity.Category;
import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

public class JavaCacheHandle {

  public static Cache cache = null;

  public static void initCache() {
    try {
      cache = CacheManager.getInstance().getCacheFactory()
          .createCache(Collections.emptyMap());
    } catch (CacheException e) {
      System.out.println(e.getMessage());
    }
  }

  public static void removeCache() {
    cache.remove("cachedKeywords");
    cache.remove("cachedCategory");
  }

  @SuppressWarnings("unchecked")
  public static List<Text> getRandomKeywords() {
    if (cache != null && cache.containsKey("cachedKeywords")) {
      List<Text> result = new ArrayList<Text>();
      List<String> cachedKeywords = (List<String>) cache
          .get("cachedKeywords");
      Random rand = new Random();
      if (cachedKeywords.size() > 0) {
        for (int i = 0; i < 30; i++) {
          Text toAdd = new Text(cachedKeywords.get(rand
              .nextInt(cachedKeywords.size())));
          if (!result.contains(toAdd)) {
            result.add(toAdd);
          }
          if (result.size() == 16) {
            break;
          }
        }
      }
      return (result.size() > 0) ? result : null;
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  public static void addKeywords(List<Text> keywords) {
    List<String> cachedKeywords = new ArrayList<String>();
    if (cache != null && cache.containsKey("cachedKeywords")) {
      cachedKeywords = (List<String>) cache.get("cachedKeywords");
    }
    for (int j = 0; j < keywords.size(); j++) {
      if (cachedKeywords.size() > 1000) {
        cachedKeywords.remove(0);
      }
      if (!cachedKeywords.contains(keywords.get(j).getValue())) {
        cachedKeywords.add(keywords.get(j).getValue());
      }
    }
    cache.put("cachedKeywords", cachedKeywords);
  }

  @SuppressWarnings("unchecked")
  public static List<Category> getCategory() {
    List<Category> result = null;
    if (cache != null && cache.containsKey("cachedCategory")) {
      result = new ArrayList<Category>();
      List<String> cachedCategory = (List<String>) cache
          .get("cachedCategory");
      if (cachedCategory.size() > 0) {
        for (String stringCate : cachedCategory) {
          Category cate = new Category();
          cate.transformString(stringCate);
          result.add(cate);
        }
      }
    }
    return result;
  }

  @SuppressWarnings("unchecked")
  public static void addCategory(List<Category> categories) {
    List<String> cachedCategory = new ArrayList<String>();
    if (cache != null && cache.containsKey("cachedCategory")) {
      System.out.println("Ok here");
      cachedCategory = (List<String>) cache.get("cachedCategory");
    }
    for (int j = 0; j < categories.size(); j++) {
      if (cachedCategory.size() > 10) {
        cachedCategory.remove(0);
      }
      if (!cachedCategory.contains(categories.get(j).toString())) {
        cachedCategory.add(categories.get(j).toString());
      }
    }
    cache.put("cachedCategory", cachedCategory);
  }

}
TOP

Related Classes of com.tubeilike.util.JavaCacheHandle

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.