当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring Data操作Redis数据库

详解Spring Data操作Redis数据库

2019年07月22日  | 移动技术网IT编程  | 我要评论

redis是一种nosql数据库,key-value形式对数据进行存储,其中数据可以以内存形式存在,也可以持久化到文件系统。spring data对redis进行了很好的封装,用起来也是十分的得心应手。redis 是一个开源(bsd许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 redis 内置了 复制(replication),lua脚本(lua scripting), lru驱动事件(lru eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 redis哨兵(sentinel)和自动 分区(cluster)提供高可用性(high availability)。

1. 系统配置,如果使用maven进行开发,只需要在pom.xml文件中添加如下配置。

<dependencies>
  <dependency>
    <groupid>org.springframework.data</groupid>
    <artifactid>spring-data-redis</artifactid>
    <version>1.8.1.release</version>
  </dependency>
</dependencies>

为了方面起见可以将spring data模板配置成 bean 方便在直接使用的地方直接注入。

<bean id="jedisconnfactory" 
  class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory" 
  p:use-pool="true"/>
<bean id="redistemplate" 
  class="org.springframework.data.redis.core.redistemplate" 
  p:connection-factory-ref="jedisconnfactory"/>

2. redis template针对不同的需求分类封装了如下操作。

opsforvalue() - operations for working with entries having simple values
opsforlist() - operations for working with entries having list values
opsforset() - operations for working with entries having set values
opsforzset() - operations for working with entries having zset (sorted set) values
opsforhash() - operations for working with entries having hash values
boundvalueops(k) - operations for working with simple values bound to a given key
boundlistops(k) - operations for working with list values bound to a given key
boundsetops(k) - operations for working with set values bound to a given key
boundzset(k) - operations for working with zset (sorted set) values bound to a given key
boundhashops(k) - operations for working with hash values bound to a given key

3. 典型操作示例

3.1 redis template注入,可以直接模板注入,也可以以ops形式注入,如下示例中对两种方式都进行了说明。

public class example {
  // inject the actual template
  @autowired
  private redistemplate<string, string> template;
  // inject the template as listoperations
  // can also inject as value, set, zset, and hashoperations
  @resource(name="redistemplate")
  private listoperations<string, string> listops;
  public void addlink(string userid, url url) {
    listops.leftpush(userid, url.toexternalform());
    // or use template directly
    template.boundlistops(userid).leftpush(url.toexternalform());
  }
}

3.2 bound系列操作示例,bound系列操作的优势在于只需要绑定一次,然后可以进行一个系列的操作,代码十分精炼。

 boundlistoperations<string, product> mangoops = redis.boundlistops("solidmango");
  product popped = mangoops.rightpop();
  mangoops.rightpush(product1);
  mangoops.rightpush(product2);
  mangoops.rightpush(product3);

3.3 serializer配置示例,通常情况下key和value都采用不同的方式进行持久化,如下示例中key使用string进行持久化,value使用jackson格式进行持久化。

@bean
public redistemplate<string, cart> redistemplate(redisconnectionfactory rcf) {
    redistemplate<string, cart> redis =
    new redistemplate<string, cart>();
    redis.setconnectionfactory(rcf);
    redis.setkeyserializer(new stringredisserializer());
    redis.setvalueserializer(
    new jackson2jsonredisserializer<product>(product.class));
    return redis;
}

总结

本文对spring data操作redis的配置和开发方式进行了详细的分析说明,配置部分给出了具体的配置方式,代码示例部分分三种情况给出了具体的解决方案,希望对大家有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网