ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
JAVA操作Redis ~~~ public static void testping() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.153",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("wtrotks"); //ping System.out.println(jedis.ping()); } public static void testString() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.131",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("123"); jedis.set("likecount", "100"); jedis.incr("likecount"); System.out.println(jedis.get("likecount")); } public static void testHash() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.131",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("123"); Map<String, String> m = new HashMap<>(); m.put("name", "feiyy"); m.put("pass", "feiyy"); m.put("age", "35"); jedis.hmset("feiyy001", m); jedis.hset("feiyy001", "email", "feiyy@neusoft.com"); Set<String> keys = jedis.hkeys("feiyy001"); for(String key :keys) { System.out.println(key); } System.out.println("===================="); List<String> values = jedis.hvals("feiyy001"); for(String value :values) { System.out.println(value); } System.out.println("===================="); Map<String, String> items = jedis.hgetAll("feiyy001"); Set<Entry<String,String>> entries = items.entrySet(); for(Entry entry :entries) { System.out.println(entry.getKey()+"=="+entry.getValue()); } } public static void testList() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.153",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("wtrotks"); jedis.lpush("imgurls", "aa.jpg"); jedis.lpush("imgurls", "bb.jpg"); jedis.lpush("imgurls", "cc.jpg"); jedis.lpush("imgurls", "dd.jpg"); jedis.ltrim("imgurls", 0, 2); List<String> list = jedis.lrange("imgurls", 0, -1); for(String item :list) { System.out.println(item); } } public static void testSet() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.153",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("wtrotks"); jedis.sadd("userlist", "james"); jedis.sadd("userlist", "jerry"); jedis.sadd("userlist", "kelly"); Set<String> users = jedis.smembers("userlist"); for(String s:users) { System.out.println(s); } System.out.println(jedis.scard("userlist")); } public static void testSortedSet() { // 创建jedis对象,如果不写端口,默认6379端口 Jedis jedis = new Jedis("10.25.39.153",6379); // 验证密码,如果没有密码,这句话不写 jedis.auth("wtrotks"); Map<String, Double> m = new HashMap<>(); m.put("movie1", 6.0); m.put("movie2", 9.8); m.put("movie3", 7.6); m.put("movie4", 7.4); jedis.zadd("movies", m); Set<String> movies = jedis.zrange("movies", 0, -1); for(String movie :movies) { System.out.println(movie); } System.out.println("================="); Set<String> movies2 = jedis.zrangeByScore("movies", 7.5, 10); for(String movie :movies2) { System.out.println(movie); } System.out.println("================="); Set<String> movies3 = jedis.zrevrange("movies", 0, 2); for(String movie :movies3) { System.out.println(movie); } } ~~~ Spring集成Redis ~~~ <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="10" /> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取连接的时候检查有效性, 默认false --> <!-- <property name="testOnBorrow" value="true" /> --> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="timeout" value="6000"></constructor-arg> <constructor-arg name="host" value="10.25.39.132"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> <constructor-arg name="password" value="123"></constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> </bean> ~~~ ~~~ public void testSpringJedisSingle() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool"); Jedis jedis = pool.getResource(); String string = jedis.get("key1"); System.out.println(string); jedis.close(); pool.close(); } ~~~ 或者在service中依赖注入: ~~~ @Autowired private JedisPool jedisPool; ~~~ 有时候我们会把对象转化为json字符串存储到redis, 从redis中取出时再转回对象,可以将这一功能封装成工具类RedisTool. ~~~ package com.neusoft.tools; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Service public class RedisTool { @Autowired private JedisPool jedisPool; private Jedis jedis; private Gson gson = new Gson(); public void saveObject(String key, Object object) { jedis = jedisPool.getResource(); String objectString = gson.toJson(object); jedis.set(key, objectString); jedis.close(); } public void deleteObject(String key) { jedis = jedisPool.getResource(); jedis.del(key); jedis.close(); } public <T> T getObject(String key,Class<T> theClass) { jedis = jedisPool.getResource(); String value = jedis.get(key); jedis.close(); if(null!=value) return gson.fromJson(value, theClass); else return null; } public <T> List<T> getObjectList(String key, Class<T> theClass){ jedis = jedisPool.getResource(); String listString = jedis.get(key); if(null!=listString) { List<T> listObject = gson.fromJson(listString, new TypeToken<List<T>>() {}.getType()); return listObject; }else return null; } } ~~~