本文内容:
为啥使用redis+redis可以做啥+redis基本命令行命令+redis在JAVA中的使用
redis的认识
1.相对于关系型数据库,redis是部署在内存里的存储机制,可以用来做缓存使用,同时,你也可以把它独立出来做成单独的缓存机制,来存储一些实时数据,比如SessionId啊,xxx的最后活动时间啊之类的,我觉得用来解决是否过期这问题挺好的
2.redis是一个非关系型数据库,和关系型数据库相比,好处就是省的麻烦,不需要建表,比如你要存一个张三这个人的信息,什么姓名啊,性别啊,出生年月,地址啊什么的,在关系型数据库中,你需要先建一张User表,然后填一行信息保存,在redis中你可以创建一个Hash类型的数据,key直接取为zhangsan,之后这个Hash中的存一系列的Field对应的Value就行了,取出来也相当方便,直接hget zhangsan Fieldname 就好了,正因为这一点,redis用来存json相当方便。
redis可以做啥
1.实时数据,也就是一时使用的数据,例如SessionID啊,一些实时榜单排名啥的,先存在redis里很快。
2.消息队列,用redis中的List类型的对象来做,一个或几个应用做生产者,一个或几个应用做消费者(也符合实时数据的定义,消息队列也就当时使用,没有太大的必要把这个存起来,有日志可以查嘛)
redis基本命令行操作
前提:
1.建起一个redis后,它被分为了好多个数据库,比如1,2,3,4,5,6等等可以选(一开始我就没看懂)
2.默认端口是6379,这个安装的时候可以看到,Linux版的不给了,windows版的
在https://github.com/MSOpenTech/redis,进去点release会出现安装包下载的
3.redis有5种存储格式,但是最后一种Sorted Set我是没用过,但是知道它开销很大
使用:
<property name ="maxIdle" value ="${redis.maxIdle}" />
<property name ="maxTotal" value ="${redis.maxActive}" />
<property name ="maxWaitMillis" value ="${redis.timeout}" />
<property name ="testOnBorrow" value ="${redis.testOnBorrow}" />
</bean>
<bean id ="shardedJedisPool" class ="redis.clients.jedis.ShardedJedisPool" >
<constructor-arg index ="0" ref ="poolConfig" />
<constructor-arg index ="1" >
<list>
<bean class ="redis.clients.jedis.JedisShardInfo" >
<constructor-arg name ="host" value ="${redis.url}" />
<property name ="soTimeout" value ="100000" />
</bean>
</list>
</constructor-arg>
redis.host =xx.xx.xx.xx
#redis.host=127.0.0.1
redis.port = 6379
redis.url = http://100.73.17.56:6379/1(这个1说的就是redis中的1号数据库)
redis.password =
redis.maxIdle = 1000
redis.maxActive = -1
redis.timeout = 100000
redis.testOnBorrow = true
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
/**
* Redis相关的服务接口
*
* @author Michael
*/
public interface RedisServer {
public static final String CRM_REDIS = "thirdpa_crm_" ;
/**
* 获得一个Redis客户端(注意,在使用完毕后需要手动close掉)
*
* @return redis客户端对象
*/
//ShardedJedis getRedisClient();
/**
* 执行一个Redis命令
*
* @param command redis命令对象
*/
< T> T execute(RedisCommand< T> command) ;
public List<String> hmget(String key , String field) ;
public String hmset(String key , Map map) ;
public void hset(String key , String field , String value) ;
public String hget(String key , String field) ;
public Long sadd(String key , String... members) ;
public String spop(String key) ;
public String set(String key , String value) ;
public String get(String key) ;
public Long del(String key) ;
public Long hdel(String key , String... fields) ;
public List<String> lrange(String key , long start , long end) ;
public String lpop(String key) ;
public String rpop(String key) ;
public Long srem(String key , String... member) ;
public Long lpush(String key , String str) ;
public Long rpush(String key , String str) ;
public Long lrem(String key ,int count ,String value) ;
public Long llen(String key) ;
public Long incrby(String key , long val) throws JedisException ;
public Set<String> hkeys(String key) ;
}
import org.apache.commons.lang.StringUtils ;
import org.apache.log4j.Logger ;
import org.springframework.stereotype.Service ;
import redis.clients.jedis.ShardedJedis ;
import redis.clients.jedis.ShardedJedisPool ;
import javax.annotation.Resource ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
/**
* @author Michael
*/
@Service
public class RedisServerImpl implements RedisServer {
@Resource
private ShardedJedisPool shardedJedisPool ;
private ShardedJedis shardedJedis ;
public RedisServerImpl() {
Logger logger = Logger. getLogger(RedisServerImpl. class) ;
logger.info( "启动Redis服务...") ;
}
private ShardedJedis getRedisClient() {
this. shardedJedis = shardedJedisPool.getResource() ;
return this. shardedJedis ;
}
public String getKey(String key) {
if (StringUtils. isNotEmpty(key)) {
return RedisServer. CRM_REDIS + key ; //这个静态字段在接口中定义了,算是唯一标识前缀
}
return null;
}
@Override
public < T> T execute(RedisCommand< T> command) {
ShardedJedis jedis = shardedJedisPool.getResource() ;
T o = command.invoke(jedis , shardedJedisPool) ;
if (! shardedJedisPool.isClosed()) {
jedis.close() ;
}
return o ;
}
public Long sadd(String key , String... members) {
this.getRedisClient() ;
Long val = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.sadd(getKey(key) , members) ;
} finally {
this.close() ;
}
}
return val ;
}
public String spop(String key) {
this.getRedisClient() ;
String val = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.spop(getKey(key)) ;
} finally {
this.close() ;
}
}
return val ;
}
@Override
public void hset(String key , String field , String value) {
this.getRedisClient() ;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
this. shardedJedis.hset(getKey(key) , field , value) ;
} finally {
this.close() ;
}
}
}
@Override
public String hget(String key , String field) {
this.getRedisClient() ;
String value = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
value = this. shardedJedis.hget(getKey(key) , field) ;
} finally {
this.close() ;
}
}
return value ;
}
@Override
public List<String> hmget(String key , String field) {
this.getRedisClient() ;
List<String> value = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
value = this. shardedJedis.hmget(getKey(key) , field) ;
} finally {
this.close() ;
}
}
return value ;
}
@Override
public String hmset(String key , Map map) {
this.getRedisClient() ;
String value = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
value = this. shardedJedis.hmset(getKey(key) , map) ;
} finally {
this.close() ;
}
}
return value ;
}
@Override
public Long hdel(String key , String... fields) {
this.getRedisClient() ;
Long val = null;
if ( this. shardedJedis != null && fields != null && fields. length > 0 && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.hdel(getKey(key) , fields) ;
} finally {
this.close() ;
}
}
return val ;
}
@Override
public List<String> lrange(String key , long start , long end) {
this.getRedisClient() ;
List<String> list = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
list = this. shardedJedis.lrange(getKey(key) , start , end) ;
} finally {
this.close() ;
}
}
return list ;
}
public Long lpush(String key , String str) {
this.getRedisClient() ;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
return this. shardedJedis.lpush(getKey(key) , str) ;
} finally {
this.close() ;
}
}
return 0L ;
}
@Override
public Long rpush(String key , String str) {
this.getRedisClient() ;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
return this. shardedJedis.rpush(getKey(key) , str) ;
} finally {
this.close() ;
}
}
return 0L ;
}
@Override
public Long llen(String key) {
this.getRedisClient() ;
Long val = null;
if(StringUtils. isEmpty(key)){
return val ;
}
if( this. shardedJedis == null){
return val ;
}
try {
val = this. shardedJedis.llen(getKey(key)) ;
} finally {
this.close() ;
}
return val ;
}
@Override
public Long lrem(String key ,int count ,String value) {
this.getRedisClient() ;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
return this. shardedJedis.lrem(getKey(key) ,count ,value) ;
} finally {
this.close() ;
}
}
return 0L ;
}
@Override
public Long incrby(String key , long val) {
this.getRedisClient() ;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
return this. shardedJedis.incrBy(getKey(key) , val) ;
} finally {
this.close() ;
}
}
return 0L ;
}
@Override
public String lpop(String key) {
this.getRedisClient() ;
String val = null;
if(StringUtils. isEmpty(key)){
return val ;
}
if( this. shardedJedis == null){
return val ;
}
try {
val = this. shardedJedis.lpop(getKey(key)) ;
} finally {
this.close() ;
}
return val ;
}
@Override
public String rpop(String key) {
this.getRedisClient() ;
String val = null;
if(StringUtils. isEmpty(key)){
return val ;
}
if( this. shardedJedis == null){
return val ;
}
try {
val = this. shardedJedis.rpop(getKey(key)) ;
} finally {
this.close() ;
}
return val ;
}
@Override
public Long srem(String key , String... member) {
this.getRedisClient() ;
Long val = null;
if(StringUtils. isEmpty(key)){
return val ;
}
if( this. shardedJedis == null){
return val ;
}
try {
val = this. shardedJedis.srem(getKey(key) , member) ;
} finally {
this.close() ;
}
return val ;
}
@Override
public String set(String key , String value) {
this.getRedisClient() ;
String val = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.set(getKey(key) , value) ;
} finally {
this.close() ;
}
}
return val ;
}
@Override
public String get(String key) {
this.getRedisClient() ;
String val = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.get(getKey(key)) ;
} finally {
this.close() ;
}
}
return val ;
}
@Override
public Long del(String key) {
this.getRedisClient() ;
Long val = null;
if ( this. shardedJedis != null && StringUtils. isNotEmpty(key)) {
try {
val = this. shardedJedis.del(getKey(key)) ;
} finally {
this.close() ;
}
}
return val ;
}
private void close() {
if ( this. shardedJedis != null && ! shardedJedisPool.isClosed()) {
try{
this. shardedJedis.close() ;
} catch (Exception e){
//e.printStackTrace();
} finally {
this. shardedJedis = null;
}
}
}
@Override
public Set<String> hkeys(String key) {
this.getRedisClient() ;
Set<String> stringSet = null;
if (StringUtils. isEmpty(key)) {
return stringSet ;
}
if( this. shardedJedis == null){
return stringSet ;
}
try {
stringSet = this. shardedJedis.hkeys(getKey(key)) ;
} finally {
this.close() ;
}
return stringSet ;
}
}