• ADADADADAD

    spring框架怎么使用redis[ 建站问答 ]

    建站问答 时间:2024-12-01 19:17:57

    作者:文/会员上传

    简介:

    "spring框架使用redis的方法:1.在pom.xml中导入redis的相关依赖,例如:<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.1.0</version>

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    "spring框架使用redis的方法:

    1.在pom.xml中导入redis的相关依赖,例如:

    <dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>2.1.0</version>

    </dependency>

    2.在applicationContext.xml中添加redis相关配置,例如:

    <!--redis配置开始-->

    <beanid=""jedisPoolConfig""class=""redis.clients.jedis.JedisPoolConfig"">

    <propertyname=""maxActive""value=""300""/>

    <propertyname=""maxIdle""value=""100""/>

    <propertyname=""maxWait""value=""1000""/>

    <propertyname=""testOnBorrow""value=""true""/>

    </bean>

    <!--ConfigpoolConfig,Stringhost,intport,inttimeout,Stringpassword,intdatabase-->

    <beanid=""jedisPool""class=""redis.clients.jedis.JedisPool""destroy-method=""destroy"">

    <constructor-argref=""jedisPoolConfig""/>

    <constructor-argvalue=""127.0.0.1""/>

    <constructor-argvalue=""6379""/>

    <constructor-argvalue=""3000""/>

    <constructor-argvalue=""123456""/>

    <constructor-argvalue=""0""/>

    </bean>

    <beanid=""redisAPI""class=""com.xc.util.RedisAPI"">

    <propertyname=""jedisPool""ref=""jedisPool""/>

    </bean>

    3.最后创建redis的工具类即可。代码如下:

    publicclassRedisAPI{

    publicstaticJedisPooljedisPool;

    publicJedisPoolgetJedisPool(){

    returnjedisPool;

    }

    publicvoidsetJedisPool(JedisPooljedisPool){

    RedisAPI.jedisPool=jedisPool;

    }

    /**

    *setkeyandvaluetoredis

    *@paramkey

    *@paramvalue

    *@return

    */

    publicstaticbooleanset(Stringkey,Stringvalue){

    try{

    Jedisjedis=jedisPool.getResource();

    jedis.set(key,value);

    returntrue;

    }catch(Exceptione){

    e.printStackTrace();

    }

    returnfalse;

    }

    /**

    *setkeyandvaluetoredis

    *@paramkey

    *@paramseconds有效期

    *@paramvalue

    *@return

    */

    publicstaticbooleanset(Stringkey,intseconds,Stringvalue){

    try{

    Jedisjedis=jedisPool.getResource();

    jedis.setex(key,seconds,value);

    returntrue;

    }catch(Exceptione){

    e.printStackTrace();

    }

    returnfalse;

    }

    /**

    *判断某个key是否存在

    *@paramkey

    *@return

    */

    publicbooleanexist(Stringkey){

    try{

    Jedisjedis=jedisPool.getResource();

    returnjedis.exists(key);

    }catch(Exceptione){

    e.printStackTrace();

    }

    returnfalse;

    }

    /**

    *返还到连接池

    *@parampool

    *@paramredis

    */

    publicstaticvoidreturnResource(JedisPoolpool,Jedisredis){

    if(redis!=null){

    pool.returnResource(redis);

    }

    }

    /**

    *获取数据

    *@paramkey

    *@return

    */

    publicStringget(Stringkey){

    Stringvalue=null;

    Jedisjedis=null;

    try{

    jedis=jedisPool.getResource();

    value=jedis.get(key);

    }catch(Exceptione){

    e.printStackTrace();

    }finally{

    //返还到连接池

    returnResource(jedisPool,jedis);

    }

    returnvalue;

    }

    /**

    *查询key的有效期,当key不存在时,返回-2。当key存在但没有设置剩余生存时间时,返回-1。否则,以秒为单位,返回key的剩余生存时间。

    *注意:在Redis2.8以前,当key不存在,或者key没有设置剩余生存时间时,命令都返回-1。

    *@paramkey

    *@return剩余多少秒

    */

    publicLongttl(Stringkey){

    try{

    Jedisjedis=jedisPool.getResource();

    returnjedis.ttl(key);

    }catch(Exceptione){

    e.printStackTrace();

    }

    return(long)-2;

    }

    /**

    *删除

    *@paramkey

    */

    publicvoiddelete(Stringkey){

    try{

    Jedisjedis=jedisPool.getResource();

    jedis.del(key);

    }catch(Exceptione){

    e.printStackTrace();

    }

    }

    }

    spring框架怎么使用redis.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: redisspring框架