package com.redis.keynotfound;
import java.io.UnsupportedEncodingException;
import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Set;import redis.clients.jedis.Builder;
import redis.clients.jedis.Client; import redis.clients.jedis.Jedis; import redis.clients.jedis.Pipeline; import redis.clients.jedis.Response; import redis.clients.jedis.Tuple; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.util.SafeEncoder;public class TestKeyNotFound {
private static Jedis jedis;
private static Client client = null;
protected static Pipeline pipeline = null;public static void initJedis() {
jedis = new Jedis("127.0.0.1", 6379, 20000); client = jedis.getClient(); }public Pipeline pipelined() {
pipeline = new Pipeline(); pipeline.setClient(client); return pipeline; }public static void main(String[] args) throws UnsupportedEncodingException {
initJedis();jedis.select(2);
// method1();
method2();}
/**
* 将乱码后的中文放入redis-key中,该怎么取值 * * UnsupportedEncodingException */ private static void method1() throws UnsupportedEncodingException { addKeyToHash(); // 根据key取出 // 转为乱码格式的字符串 String s = "天津"; byte[] n2 = s.getBytes("gb2312"); String gn2 = new String(n2, "utf-8"); // 应该用对应的乱码去取 Set<Tuple> res = jedis.zrangeWithScores("hot_projects_" + gn2, 0, -1); Iterator<Tuple> it = res.iterator(); while (it.hasNext()) { Tuple t = it.next();System.out.println("element:" + t.getElement().toString() + ",score:" + t.getScore());
} }private static void addKeyToHash() throws UnsupportedEncodingException {
String name = "天津"; // 插入一条 byte[] n1 = name.getBytes("gb2312"); String gn = new String(n1, "utf-8"); System.out.println(gn); Map<String, Double> scoreMembers = new HashMap<String, Double>(); scoreMembers.put("怡景花园", Double.valueOf(5000)); scoreMembers.put("新福家园", Double.valueOf(500010)); jedis.del("hot_projects_" + gn); jedis.zadd("hot_projects_" + gn, scoreMembers); }public static void method2() {
String s = "hot_projects_" + "天津"; byte[] ss = encode(s, "gb2312"); s = SafeEncoder.encode(ss); Set<Tuple> res = zrangeWithScores(SafeEncoder.encode(s), 0, -1); for (Tuple t : res) { System.out.println("element:" + t.getElement().toString() + ",score:" + t.getScore()); } }private static byte[] encode(final String str, String encodeType) {
try { if (str == null) { throw new JedisDataException("value sent to redis cannot be null"); } return str.getBytes(encodeType); } catch (UnsupportedEncodingException e) { throw new JedisException(e); } }private static Set<Tuple> zrangeWithScores(final byte[] key, final long start, final long end) {
client.zrangeWithScores(key, start, end); return getTupledSet(); }private static Set<Tuple> getTupledSet() {
List<String> membersWithScores = client.getMultiBulkReply(); if (membersWithScores == null) { return null; } if (membersWithScores.isEmpty()) { return Collections.emptySet(); } Set<Tuple> set = new LinkedHashSet<Tuple>(membersWithScores.size() / 2, 1.0f); Iterator<String> iterator = membersWithScores.iterator(); while (iterator.hasNext()) { set.add(new Tuple(iterator.next(), Double.valueOf(iterator.next()))); } return set; }public class Queable {
private Queue<Response<?>> pipelinedResponses = new LinkedList<Response<?>>();protected void clean() {
pipelinedResponses.clear(); }protected Response<?> generateResponse(Object data) {
Response<?> response = pipelinedResponses.poll(); if (response != null) { response.set(data); } return response; }protected <T> Response<T> getResponse(Builder<T> builder) {
Response<T> lr = new Response<T>(builder); pipelinedResponses.add(lr); return lr; }protected boolean hasPipelinedResponse() {
return !pipelinedResponses.isEmpty(); }protected int getPipelinedResponseLength() {
return pipelinedResponses.size(); } } }