redis数据库的java接入,redisson的使用
1、首先可以百度redis看一下官网了解一下,不过redis的官网对于这次经验来说并没有太大的用处

2、redis采用c语言编写,所以用在java上并不是直接使用的,而是去接入一个叫做redisson的项目,这个项目可以在github上找到

3、从redisson项目中下载我们需要的jar包,另外需要关联源码的小伙伴可以顺便把源码下载一下,打成zip包放在eclipse中进行源码关联,方便追源码


4、在redis官网上找到windows版本的service安装包,位置是在这里,然后会导航到由微软维护的redis windows项目中,找到realease页面,然后在下这个稳定版的zip包




5、然后这里就可以打开redis的本地service服务了,就像mysql之类的数据库一样


6、为了使用更加方便,我们可以去下载一个redis的可视化工具,下载安装之后,连接到本地的service即可看到所有数据库表


7、这是测试所用到的类:
package db;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import org.redisson.Redisson;
import org.redisson.config.Config;
public class RedisExample {
/**
* @param args
*/
public static void main(String[] args) {
// 1.初始化配置
Config config = new Config();
config.useSingleServer().setAddress("http://127.0.0.1:6379"); //要连接的redis库
Redisson redisson = (Redisson) Redisson.create(config); //创建连接
System.out.println("reids连接成功...");
// 2.测试concurrentMap,put方法的时候就会同步到redis中 ,在redis中创建了一张FirstMap表
ConcurrentMap<String, Object> map = redisson.getMap("FirstMap");
map.put("wuguowei", "男");
map.put("zhangsan", "nan");
map.put("lisi", "女");
ConcurrentMap resultMap = redisson.getMap("FirstMap");
System.out.println("resultMap==" + resultMap.keySet());
// 2.测试Set集合 ,创建MySet的set集合表
Set mySet = redisson.getSet("MySet");
mySet.add("wuguowei");
mySet.add("lisi");
Set resultSet = redisson.getSet("MySet");
System.out.println("resultSet===" + resultSet.size());
//3.测试Queue队列
Queue myQueue = redisson.getQueue("FirstQueue");
myQueue.add("wuguowei");
myQueue.add("lili");
myQueue.add("zhangsan");
Queue resultQueue=redisson.getQueue("FirstQueue");
System.out.println("resultQueue==="+resultQueue);
// 关闭连接
redisson.shutdown();
}
}


8、运行之后会发现成功链接到数据库,不过出了一些小问题,主要日志包重复,这里是由于我们的redis是和webmagic结合使用的,两个都有slf4j的日志包的缘故,这里我选择删除redis的jar中的slf4j包,我们用winrar打开redis.jar,然后找到里面的slf4j目录并删除,这时得到一个zip文件,然后再将zip文件的后缀名改为.jar即可




9、ps,于2017/09/07补充:
这是我自己写的一个简单操作redis数据库的Dao类,可以作为参考
package db;
import java.util.Set;
import org.redisson.Redisson;
import org.redisson.config.Config;
public class RedisDao {
public static int STATUS = 0;
private static Redisson redisson;
static {
//启动redis服务器
openExe();
// 1.初始化配置
Config config = new Config();
config.useSingleServer().setAddress("http://127.0.0.1:6379"); //要连接的redis库
redisson = (Redisson) Redisson.create(config); //创建连接
System.out.println("reids连接成功...");
}
public static void add(String setName,String url) {
// 将url加入set表中
Set mySet = redisson.getSet(setName);
mySet.add(url);
}
public static boolean querySet(String setName,String url) {
// 查询set中是否包含url
Set mySet = redisson.getSet(setName);
return mySet.contains(url);
}
public static void closeRedis() {
// 关闭连接
redisson.shutdown();
}
// 调用可执行文件
public static void openExe() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
//redis-server.exe的路径
process = runtime.exec("G:\\eclipse4.7space\\Redis-x64-3.0.504\\redis-server.exe");
STATUS = 1;
} catch (final Exception e) {
System.out.println("Error exec!");
}
}
}

10、另外需要注意一下redis的默认持久化规则,单key操作完成后15min后保存,10key操作完成5min后保存,10000key操作完成后,1min后保存,且以上保存只发生在操作完成之后,持续操作中断电将导致数据丢失
# Save the DB on disk:
#
# save <seconds> <changes>
#
# Will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after 900 sec (15 min) if at least 1 key changed
# after 300 sec (5 min) if at least 10 keys changed
# after 60 sec if at least 10000 keys changed
#
# Note: you can disable saving completely by commenting out all "save" lines.
#
# It is also possible to remove all the previously configured save
# points by adding a save directive with a single empty string argument
# like in the following example:
#
# save ""
save 900 1
save 300 10
save 60 10000
