Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/95 update java redis library #104

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.swp
/java/build
/java/.gradle
GeoLiteCity-*.csv
.idea
*.iml
Expand Down
2 changes: 1 addition & 1 deletion java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {

dependencies {

compile 'redis.clients:jedis:2.1.0'
compile 'redis.clients:jedis:4.3.2'
compile 'org.javatuples:javatuples:1.2'
compile 'com.google.code.gson:gson:2.2.2'

Expand Down
6 changes: 3 additions & 3 deletions java/src/main/java/Chapter01.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.ZParams;

import java.util.*;

Expand All @@ -13,7 +13,7 @@ public static final void main(String[] args) {
}

public void run() {
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
conn.select(15);

String articleId = postArticle(
Expand Down Expand Up @@ -88,7 +88,7 @@ public List<Map<String,String>> getArticles(Jedis conn, int page, String order)
int start = (page - 1) * ARTICLES_PER_PAGE;
int end = start + ARTICLES_PER_PAGE - 1;

Set<String> ids = conn.zrevrange(order, start, end);
List<String> ids = conn.zrevrange(order, start, end);
List<Map<String,String>> articles = new ArrayList<Map<String,String>>();
for (String id : ids){
Map<String,String> articleData = conn.hgetAll(id);
Expand Down
22 changes: 11 additions & 11 deletions java/src/main/java/Chapter02.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import com.google.gson.Gson;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.resps.Tuple;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -16,7 +16,7 @@ public static final void main(String[] args)
public void run()
throws InterruptedException
{
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
conn.select(15);

testLoginCookies(conn);
Expand Down Expand Up @@ -103,7 +103,7 @@ public void testCacheRows(Jedis conn)
System.out.println("First, let's schedule caching of itemX every 5 seconds");
scheduleRowCache(conn, "itemX", 5);
System.out.println("Our schedule looks like:");
Set<Tuple> s = conn.zrangeWithScores("schedule:", 0, -1);
List<Tuple> s = conn.zrangeWithScores("schedule:", 0, -1);
for (Tuple tuple : s){
System.out.println(" " + tuple.getElement() + ", " + tuple.getScore());
}
Expand Down Expand Up @@ -263,7 +263,7 @@ public class CleanSessionsThread
private boolean quit;

public CleanSessionsThread(int limit) {
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
this.limit = limit;
}
Expand All @@ -285,8 +285,8 @@ public void run() {
}

long endIndex = Math.min(size - limit, 100);
Set<String> tokenSet = conn.zrange("recent:", 0, endIndex - 1);
String[] tokens = tokenSet.toArray(new String[tokenSet.size()]);
List<String> tokenList = conn.zrange("recent:", 0, endIndex - 1);
String[] tokens = tokenList.toArray(new String[tokenList.size()]);

ArrayList<String> sessionKeys = new ArrayList<String>();
for (String token : tokens) {
Expand All @@ -308,7 +308,7 @@ public class CleanFullSessionsThread
private boolean quit;

public CleanFullSessionsThread(int limit) {
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
this.limit = limit;
}
Expand All @@ -330,8 +330,8 @@ public void run() {
}

long endIndex = Math.min(size - limit, 100);
Set<String> sessionSet = conn.zrange("recent:", 0, endIndex - 1);
String[] sessions = sessionSet.toArray(new String[sessionSet.size()]);
List<String> sessionList = conn.zrange("recent:", 0, endIndex - 1);
String[] sessions = sessionList.toArray(new String[sessionList.size()]);

ArrayList<String> sessionKeys = new ArrayList<String>();
for (String sess : sessions) {
Expand All @@ -353,7 +353,7 @@ public class CacheRowsThread
private boolean quit;

public CacheRowsThread() {
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
}

Expand All @@ -364,7 +364,7 @@ public void quit() {
public void run() {
Gson gson = new Gson();
while (!quit){
Set<Tuple> range = conn.zrangeWithScores("schedule:", 0, 0);
List<Tuple> range = conn.zrangeWithScores("schedule:", 0, 0);
Tuple next = range.size() > 0 ? range.iterator().next() : null;
long now = System.currentTimeMillis() / 1000;
if (next == null || next.getScore() > now){
Expand Down
9 changes: 4 additions & 5 deletions java/src/main/java/Chapter04.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.resps.Tuple;

import java.lang.reflect.Method;
import java.util.List;
Expand All @@ -14,7 +14,7 @@ public static final void main(String[] args) {
}

public void run() {
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
conn.select(15);

testListItem(conn, false);
Expand Down Expand Up @@ -44,7 +44,7 @@ public void testListItem(Jedis conn, boolean nested) {
boolean l = listItem(conn, item, seller, 10);
System.out.println("Listing the item succeeded? " + l);
assert l;
Set<Tuple> r = conn.zrangeWithScores("market:", 0, -1);
List<Tuple> r = conn.zrangeWithScores("market:", 0, -1);
System.out.println("The market contains:");
for (Tuple tuple : r){
System.out.println(" " + tuple.getElement() + ", " + tuple.getScore());
Expand Down Expand Up @@ -201,14 +201,13 @@ public void updateToken(Jedis conn, String token, String user, String item) {
public void updateTokenPipeline(Jedis conn, String token, String user, String item) {
long timestamp = System.currentTimeMillis() / 1000;
Pipeline pipe = conn.pipelined();
pipe.multi();
pipe.hset("login:", token, user);
pipe.zadd("recent:", timestamp, token);
if (item != null){
pipe.zadd("viewed:" + token, timestamp, item);
pipe.zremrangeByRank("viewed:" + token, 0, -26);
pipe.zincrby("viewed:", -1, item);
}
pipe.exec();
pipe.sync();
}
}
24 changes: 14 additions & 10 deletions java/src/main/java/Chapter05.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import com.google.gson.reflect.TypeToken;
import org.apache.commons.csv.CSVParser;
import org.javatuples.Pair;
import redis.clients.jedis.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.resps.Tuple;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.params.ZParams;

import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -36,7 +40,7 @@ public static final void main(String[] args)
public void run()
throws InterruptedException
{
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
conn.select(15);

testLogRecent(conn);
Expand Down Expand Up @@ -74,7 +78,7 @@ public void testLogCommon(Jedis conn) {
logCommon(conn, "test", "message-" + count);
}
}
Set<Tuple> common = conn.zrevrangeWithScores("common:test:info", 0, -1);
List<Tuple> common = conn.zrevrangeWithScores("common:test:info", 0, -1);
System.out.println("The current number of common messages is: " + common.size());
System.out.println("Those common messages are:");
for (Tuple tuple : common){
Expand Down Expand Up @@ -149,7 +153,7 @@ public void testAccessTime(Jedis conn)
timer.stop("req-" + i);
}
System.out.println("The slowest access times are:");
Set<Tuple> atimes = conn.zrevrangeWithScores("slowest:AccessTime", 0, -1);
List<Tuple> atimes = conn.zrevrangeWithScores("slowest:AccessTime", 0, -1);
for (Tuple tuple : atimes){
System.out.println(" " + tuple.getElement() + ", " + tuple.getScore());
}
Expand Down Expand Up @@ -355,7 +359,7 @@ public List<Object> updateStats(Jedis conn, String context, String type, double
public Map<String,Double> getStats(Jedis conn, String context, String type){
String key = "stats:" + context + ':' + type;
Map<String,Double> stats = new HashMap<String,Double>();
Set<Tuple> data = conn.zrangeWithScores(key, 0, -1);
List<Tuple> data = conn.zrangeWithScores(key, 0, -1);
for (Tuple tuple : data){
stats.put(tuple.getElement(), tuple.getScore());
}
Expand Down Expand Up @@ -418,7 +422,7 @@ public Map<String,Object> getConfig(Jedis conn, String type, String component) {
public Jedis redisConnection(String component){
Jedis configConn = REDIS_CONNECTIONS.get("config");
if (configConn == null){
configConn = new Jedis("localhost");
configConn = new Jedis("redis://localhost:6379");
configConn.select(15);
REDIS_CONNECTIONS.put("config", configConn);
}
Expand All @@ -428,7 +432,7 @@ public Jedis redisConnection(String component){
Map<String,Object> config = getConfig(configConn, "redis", component);

if (!config.equals(oldConfig)){
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
if (config.containsKey("db")){
conn.select(((Double)config.get("db")).intValue());
}
Expand Down Expand Up @@ -519,7 +523,7 @@ public String randomOctet(int max) {

public String[] findCityByIp(Jedis conn, String ipAddress) {
int score = ipToScore(ipAddress);
Set<String> results = conn.zrevrangeByScore("ip2cityid:", score, 0, 0, 1);
List<String> results = conn.zrevrangeByScore("ip2cityid:", score, 0, 0, 1);
if (results.size() == 0) {
return null;
}
Expand All @@ -538,7 +542,7 @@ public class CleanCountersThread
private long timeOffset; // used to mimic a time in the future.

public CleanCountersThread(int sampleCount, long timeOffset){
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
this.sampleCount = sampleCount;
this.timeOffset = timeOffset;
Expand All @@ -554,7 +558,7 @@ public void run(){
long start = System.currentTimeMillis() + timeOffset;
int index = 0;
while (index < conn.zcard("known:")){
Set<String> hashSet = conn.zrange("known:", index, index);
List<String> hashSet = conn.zrange("known:", index, index);
index++;
if (hashSet.size() == 0) {
break;
Expand Down
21 changes: 10 additions & 11 deletions java/src/main/java/Chapter06.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import com.google.gson.reflect.TypeToken;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.resps.Tuple;
import redis.clients.jedis.params.ZParams;

import java.io.*;
import java.util.*;
Expand All @@ -20,7 +20,7 @@ public static final void main(String[] args)
public void run()
throws InterruptedException, IOException
{
Jedis conn = new Jedis("localhost");
Jedis conn = new Jedis("redis://localhost:6379");
conn.select(15);

testAddUpdateContact(conn);
Expand Down Expand Up @@ -586,8 +586,7 @@ public String sendMessage(Jedis conn, String chatId, String sender, String messa

@SuppressWarnings("unchecked")
public List<ChatMessages> fetchPendingMessages(Jedis conn, String recipient) {
Set<Tuple> seenSet = conn.zrangeWithScores("seen:" + recipient, 0, -1);
List<Tuple> seenList = new ArrayList<Tuple>(seenSet);
List<Tuple> seenList = conn.zrangeWithScores("seen:" + recipient, 0, -1);

Transaction trans = conn.multi();
for (Tuple tuple : seenList){
Expand Down Expand Up @@ -628,10 +627,10 @@ public List<ChatMessages> fetchPendingMessages(Jedis conn, String recipient) {
conn.zadd("chat:" + chatId, seenId, recipient);
seenUpdates.add(new Object[]{"seen:" + recipient, seenId, chatId});

Set<Tuple> minIdSet = conn.zrangeWithScores("chat:" + chatId, 0, 0);
if (minIdSet.size() > 0){
List<Tuple> minIdList = conn.zrangeWithScores("chat:" + chatId, 0, 0);
if (minIdList.size() > 0){
msgRemoves.add(new Object[]{
"msgs:" + chatId, minIdSet.iterator().next().getScore()});
"msgs:" + chatId, minIdList.iterator().next().getScore()});
}
chatMessages.add(new ChatMessages(chatId, messages));
}
Expand Down Expand Up @@ -779,7 +778,7 @@ public class PollQueueThread
private Gson gson = new Gson();

public PollQueueThread(){
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
}

Expand All @@ -789,7 +788,7 @@ public void quit() {

public void run() {
while (!quit){
Set<Tuple> items = conn.zrangeWithScores("delayed:", 0, 0);
List<Tuple> items = conn.zrangeWithScores("delayed:", 0, 0);
Tuple item = items.size() > 0 ? items.iterator().next() : null;
if (item == null || item.getScore() > System.currentTimeMillis()) {
try{
Expand Down Expand Up @@ -829,7 +828,7 @@ public class CopyLogsThread
private long limit;

public CopyLogsThread(File path, String channel, int count, long limit) {
this.conn = new Jedis("localhost");
this.conn = new Jedis("redis://localhost:6379");
this.conn.select(15);
this.path = path;
this.channel = channel;
Expand Down
Loading