Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ownxile.util.web.vote;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- /**
- * @author Robbie
- *
- */
- public class VoteCache {
- private final static int HOUR_IN_MILLIS = 3600000;
- private final static int HOURS_PER_CLAIM = 12;
- private static ArrayList<CachedVoteClaim> cachedVotes = new ArrayList<CachedVoteClaim>();
- @SuppressWarnings("unused")
- private static final File VOTE_FILE = new File(
- "./etc/data/vote/vote_cache.dat");
- public static void addClaimToCache(String playerName) {
- cachedVotes.add(new CachedVoteClaim(playerName));
- }
- public static boolean canVote(String userName) {
- for (CachedVoteClaim cached : cachedVotes) {
- if (cached.getUserName() == userName
- && System.currentTimeMillis() - cached.getVoteTimeMillis() < HOUR_IN_MILLIS
- * HOURS_PER_CLAIM) {
- return false;
- }
- }
- return true;
- }
- public static void purgeCache() {
- int i = 0;
- for (CachedVoteClaim cached : cachedVotes) {
- if (System.currentTimeMillis() - cached.getVoteTimeMillis() < HOUR_IN_MILLIS
- * HOURS_PER_CLAIM) {
- cachedVotes.remove(cached);
- i++;
- }
- }
- if (i > 0)
- System.out.println("Cleared " + i + " cached votes.");
- }
- @SuppressWarnings("unchecked")
- public static void loadCache(File file) {
- try {
- FileInputStream fileIn = new FileInputStream(file);
- ObjectInputStream in = new ObjectInputStream(fileIn);
- try {
- cachedVotes = (ArrayList<CachedVoteClaim>) in.readObject();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- in.close();
- fileIn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void saveCache(ArrayList<CachedVoteClaim> voteCache, File file) {
- try {
- FileOutputStream fileOut = new FileOutputStream(file);
- ObjectOutputStream out = new ObjectOutputStream(fileOut);
- out.writeObject(voteCache);
- out.close();
- fileOut.close();
- } catch (IOException i) {
- i.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement