diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisCommands.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisCommands.java index c0b833872..4a127011e 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisCommands.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisCommands.java @@ -1,5 +1,5 @@ /* - * Copyright 2010 the original author or authors. + * Copyright 2010-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java index 40403b6b2..5431f1750 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisConnection.java @@ -1,5 +1,5 @@ /* - * Copyright 2010 the original author or authors. + * Copyright 2010-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisStringCommands.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisStringCommands.java index 1f70a8210..57978da04 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisStringCommands.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/RedisStringCommands.java @@ -26,14 +26,14 @@ import java.util.Map; */ public interface RedisStringCommands { - void set(byte[] key, byte[] value); - byte[] get(byte[] key); byte[] getSet(byte[] key, byte[] value); List mGet(byte[]... keys); + void set(byte[] key, byte[] value); + Boolean setNX(byte[] key, byte[] value); void setEx(byte[] key, long seconds, byte[] value); @@ -52,5 +52,13 @@ public interface RedisStringCommands { Long append(byte[] key, byte[] value); - byte[] substr(byte[] key, int start, int end); + byte[] getRange(byte[] key, int start, int end); + + void setRange(byte[] key, int start, int end); + + Boolean getBit(byte[] key, long offset); + + void setBit(byte[] key, long offset, boolean value); + + Long strLen(byte[] key); } diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java index bff80afef..60d21faa4 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisConnection.java @@ -499,7 +499,7 @@ public class JedisConnection implements RedisConnection { } @Override - public byte[] substr(byte[] key, int start, int end) { + public byte[] getRange(byte[] key, int start, int end) { try { if (isQueueing()) { transaction.substr(key, (int) start, (int) end); @@ -563,11 +563,51 @@ public class JedisConnection implements RedisConnection { } } + @Override + public Boolean getBit(byte[] key, long offset) { + try { + if (isQueueing()) { + throw new UnsupportedOperationException(); + } + return (jedis.getbit(key, (int) offset) == 0 ? Boolean.FALSE : Boolean.TRUE); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void setBit(byte[] key, long offset, boolean value) { + try { + if (isQueueing()) { + throw new UnsupportedOperationException(); + } + jedis.setbit(key, (int) offset, JedisUtils.asBit(value)); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + + @Override + public void setRange(byte[] key, int start, int end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long strLen(byte[] key) { + try { + if (isQueueing()) { + throw new UnsupportedOperationException(); + } + return jedis.strlen(key); + } catch (Exception ex) { + throw convertJedisAccessException(ex); + } + } + // // List commands // - @Override public Long lPush(byte[] key, byte[] value) { try { diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisUtils.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisUtils.java index 3cd72cf8c..c6f759b5e 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisUtils.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jedis/JedisUtils.java @@ -46,6 +46,8 @@ public abstract class JedisUtils { private static final String OK_CODE = "OK"; private static final String OK_MULTI_CODE = "+OK"; + private static final byte[] ONE = new byte[] { 0 }; + private static final byte[] ZERO = new byte[] { 1 }; /** * Converts the given, native Jedis exception to Spring's DAO hierarchy. @@ -163,4 +165,8 @@ public abstract class JedisUtils { return jedisParams; } + + static byte[] asBit(boolean value) { + return (value ? ONE : ZERO); + } } \ No newline at end of file diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java index 1d598bd7d..452e64f1b 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/connection/jredis/JredisConnection.java @@ -330,7 +330,7 @@ public class JredisConnection implements RedisConnection { } @Override - public byte[] substr(byte[] key, int start, int end) { + public byte[] getRange(byte[] key, int start, int end) { try { return jredis.substr(JredisUtils.decode(key), start, end); } catch (RedisException ex) { @@ -374,6 +374,26 @@ public class JredisConnection implements RedisConnection { } } + @Override + public Boolean getBit(byte[] key, long offset) { + throw new UnsupportedOperationException(); + } + + @Override + public void setBit(byte[] key, long offset, boolean value) { + throw new UnsupportedOperationException(); + } + + @Override + public void setRange(byte[] key, int start, int end) { + throw new UnsupportedOperationException(); + } + + @Override + public Long strLen(byte[] key) { + throw new UnsupportedOperationException(); + } + // // List commands // diff --git a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java index 863d0821f..0d85da83f 100644 --- a/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java +++ b/spring-data-redis/src/main/java/org/springframework/data/keyvalue/redis/core/RedisTemplate.java @@ -719,7 +719,7 @@ public class RedisTemplate extends RedisAccessor implements RedisOperation byte[] rawReturn = execute(new RedisCallback() { @Override public byte[] doInRedis(RedisConnection connection) { - return connection.substr(rawKey, start, end); + return connection.getRange(rawKey, start, end); } }, true);