Package com.tubeonfire.util

Source Code of com.tubeonfire.util.JavaCacheHandle

package com.tubeonfire.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.tubeonfire.entity.Category;
import com.tubeonfire.entity.Channel;
import com.tubeonfire.entity.Playlist;
import com.tubeonfire.entity.Tube;
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("cachedChannels");
    cache.remove("cachedMostViewToday");
    cache.remove("cachedMostViewAllTime");
    cache.remove("cachedPopular");
    cache.remove("cachedRecent");
    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<Channel> getChannels() {
    List<Channel> result = null;
    if (cache != null && cache.containsKey("cachedChannels")) {
      result = new ArrayList<Channel>();
      List<String> cachedChannels = (List<String>) cache
          .get("cachedChannels");
      if (cachedChannels.size() > 0) {
        for (String stringChannel : cachedChannels) {
          Channel channel = new Channel();
          channel.transformString(stringChannel);
          result.add(channel);
        }
      }
    }
    return result;
  }

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

  @SuppressWarnings("unchecked")
  public static List<Tube> getMostViewToday() {
    List<Tube> result = null;
    if (cache != null && cache.containsKey("cachedMostViewToday")) {
      result = new ArrayList<Tube>();
      List<String> cachedTubes = (List<String>) cache
          .get("cachedMostViewToday");
      if (cachedTubes.size() > 0) {
        for (String stringChannel : cachedTubes) {
          Tube tub = new Tube();
          tub.transformString(stringChannel);
          result.add(tub);
        }
      }
    }
    return result;
  }

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

  @SuppressWarnings("unchecked")
  public static List<Tube> getMostViewAllTime() {
    List<Tube> result = null;
    if (cache != null && cache.containsKey("cachedMostViewAllTime")) {
      result = new ArrayList<Tube>();
      List<String> cachedTubes = (List<String>) cache
          .get("cachedMostViewAllTime");
      if (cachedTubes.size() > 0) {
        for (String stringChannel : cachedTubes) {
          Tube tub = new Tube();
          tub.transformString(stringChannel);
          result.add(tub);
        }
      }
    }
    return result;
  }

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

  @SuppressWarnings("unchecked")
  public static List<Tube> getPopular() {
    List<Tube> result = null;
    if (cache != null && cache.containsKey("cachedPopular")) {
      result = new ArrayList<Tube>();
      List<String> cachedTubes = (List<String>) cache
          .get("cachedPopular");
      if (cachedTubes.size() > 0) {
        for (String stringChannel : cachedTubes) {
          Tube tub = new Tube();
          tub.transformString(stringChannel);
          result.add(tub);
        }
      }
    }
    return result;
  }

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

  @SuppressWarnings("unchecked")
  public static List<Tube> getRecent() {
    List<Tube> result = null;
    if (cache != null && cache.containsKey("cachedRecent")) {
      result = new ArrayList<Tube>();
      List<String> cachedTubes = (List<String>) cache.get("cachedRecent");
      if (cachedTubes.size() > 0) {
        for (String stringChannel : cachedTubes) {
          Tube tub = new Tube();
          tub.transformString(stringChannel);
          result.add(tub);
        }
      }
    }
    return result;
  }

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

  @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);
  }

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

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

Related Classes of com.tubeonfire.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.