redis操作实战

一、基本存储结构

1.hash

127.0.0.1:6379> keys *ServiceConfig*
1) "NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d"
2) "com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem.serviceTypeKey.NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:97b772b4-3031-4788-936a-127df9ee5a47"
3) "NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:8f18d57d-b0c9-4b5b-b503-8ed04b707b87"
4) "com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem.serviceTypeKey.NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:f047c363-189f-4921-922b-73aa40a194d1"
5) "NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:All"

列出测试对象,以serviceconfig为例,首先查看对象类型:
127.0.0.1:6379> type NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d
hash

利用hash来存储对象,查询所有hash所有字段:
127.0.0.1:6379> hgetall NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d
 1) "serviceDefinedTransportListKey"
 2) "\"NGMA_SYSTEM:cbb6c789-ea35-4b05-b97e-aab8e6d5318d:serviceDefinedTransport\""
 3) "nameSpace"
 4) "{\"productName\":\"NGMA_SYSTEM\"}"
 5) "serviceTypeKey"
 6) "\"NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:f047c363-189f-4921-922b-73aa40a194d1\""
 7) "extendedAttr"
 8) "{}"
 9) "key"
10) "\"NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d\""
11) "uuid"
12) "\"cbb6c789-ea35-4b05-b97e-aab8e6d5318d\""
13) "keyAll"
14) "\"NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:All\""
15) "serviceNodeKey"
16) "\"NGMA_SYSTEM:com.polycom.rpum.core.shared.dataobject.NgmaServiceNode:763b79fa-8e5d-47e8-8dd3-73354a6be977\""
17) "serviceName"
18) "\"pd-rpp-tomcat-default\""

如果查看单个字段,如下命令:
hget  hashname  fieldname

127.0.0.1:6379> hget NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d   serviceDefinedTransportListKey
"\"NGMA_SYSTEM:cbb6c789-ea35-4b05-b97e-aab8e6d5318d:serviceDefinedTransport\""

基本对象容易,下面看看如果是对象如何处理?? 存储的是对应对象的key,利用key去查询,type  transport等

下面以service type为例,必然也是hash存储形式,如下查看
127.0.0.1:6379> type NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:f047c363-189f-4921-922b-73aa40a194d1
hash

127.0.0.1:6379> hgetall NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:f047c363-189f-4921-922b-73aa40a194d1
 1) "startTimeout"
 2) "60"
 3) "serviceTypeFlag"
 4) "\"BUSINESS\""
 5) "keyAll"
 6) "\"NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:All\""
 7) "serviceTypeName"
 8) "\"pd-rpp-tomcat\""
 9) "dependencyUuidStringListKey"
10) "\"NGMA_SYSTEM:f047c363-189f-4921-922b-73aa40a194d1:dependencyList\""
11) "uuid"
12) "\"f047c363-189f-4921-922b-73aa40a194d1\""
13) "key"
14) "\"NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceTypeMem:f047c363-189f-4921-922b-73aa40a194d1\""
15) "loadOnStartup"
16) "true"
17) "defaultTransportListKey"
18) "\"NGMA_SYSTEM:f047c363-189f-4921-922b-73aa40a194d1:defaultTransport\""
19) "stopCommandTemplate"
20) "\"/opt/polycom/rpum/current/bin/test/demo-stop-platform-directory.sh\""
21) "reportStatus"
22) "true"
23) "startupCommandTemplate"
24) "\"/opt/polycom/rpum/current/bin/test/demo-launch-platform-directory.sh\""
25) "stopTimeout"
26) "60"
27) "nameSpace"
28) "{\"productName\":\"NGMA_SYSTEM\"}"
29) "extendedAttr"
30) "{\"restartIfNoStatusReport\":\"false\",\"defaultInstanceID\":\"cbb6c789-ea35-4b05-b97e-aab8e6d5318d\",\"mlaxRestartCount\":\"3\",\"managedService\":\"true\"}"
31) "restart"
32) "false"
33) "permissionListKey"
34) "\"NGMA_SYSTEM:f047c363-189f-4921-922b-73aa40a194d1:permission\""


127.0.0.1:6379> hset 001 name  "kerry"
(integer) 1
127.0.0.1:6379> hget  001
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget 001:name
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget 001 name
"kerry"


127.0.0.1:6379> hmset user:001 name "kerry" age 18 birthday "20010101"
OK
127.0.0.1:6379> hget user:001  name
"kerry"
127.0.0.1:6379> hget user:001  age
"18"
127.0.0.1:6379> hget user:001  birthday
"20010101"
127.0.0.1:6379> hgetall user:001
1) "name"
2) "kerry"
3) "age"
4) "18"
5) "birthday"
6) "20010101"

2. list 

127.0.0.1:6379> lrange NGMA_SYSTEM:cbb6c789-ea35-4b05-b97e-aab8e6d5318d:serviceDefinedTransport  0  100
1) "NGMA_SYSTEM:com.polycom.rpum.core.shared.dataobject.NgmaTransport:5d886988-6a72-424a-92d3-df45876442b5"
2) "NGMA_SYSTEM:com.polycom.rpum.core.shared.dataobject.NgmaTransport:ac39935b-f4fa-4341-9d1c-dfa9a8806526"

3. string

get  set  

4. set

127.0.0.1:6379> type NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:All
set
127.0.0.1:6379> hgetall NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:All
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> smembers NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:All
1) "NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:8f18d57d-b0c9-4b5b-b503-8ed04b707b87"
2) "NGMA_SYSTEM:com.polycom.rpum.core.dal.memi.NgmaServiceConfigMem:cbb6c789-ea35-4b05-b97e-aab8e6d5318d"


5 Sort Set


not found in our project

127.0.0.1:6379> zadd  a
(error) ERR wrong number of arguments for 'zadd' command
127.0.0.1:6379> zadd  a  ab
(error) ERR wrong number of arguments for 'zadd' command
127.0.0.1:6379> zadd  student   kerry  100
(error) ERR value is not a valid float
127.0.0.1:6379> zadd  student     100  kerry
(integer) 1
127.0.0.1:6379> zadd student  80  addsion
(integer) 1
127.0.0.1:6379> zrange  0  -1
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange  student  0  -1
1) "addsion"
2) "kerry"
127.0.0.1:6379> zrange  student  0  -1  withscores
1) "addsion"
2) "80"
3) "kerry"
4) "100"

二、使用分析

2.1  Get All 操作,以service config为例

应用层调用---------->DAL-------------->redis layer
 
List serviceConfigList = servicedata.getAllServiceConfigs();



   public List getAllServiceConfigs()
        throws PlcmStatusException
    {
        final List serviceConfigList = new ArrayList<>();
        logTime(() -> {
            serviceConfigList.addAll(serviceDataMemDao.getAllServiceConfigs());
            return "serviceDataMemDao.getAllServiceConfigs()";
        });
        if (serviceConfigList.size() == 0)
        {
            loadServiceConfigToMem();
            logTime(() -> {
                serviceConfigList.addAll(serviceDataMemDao.getAllServiceConfigs());
                return "serviceDataMemDao.getAllServiceConfigs()";
            });
        }
        return serviceConfigList;
    }
 
   
  @Override
    public List getAllServiceConfigs()
        throws PlcmStatusException
    {
        List serviceConfigList = new ArrayList<>();
        NgmaServiceConfigMem mem = new NgmaServiceConfigMem();
        Set serviceConfigKeys = memoryService.memorySmembers(mem.getKeyAll());


        if (!serviceConfigKeys.isEmpty())
        {
            Iterator iter = serviceConfigKeys.iterator();
            while (iter.hasNext())
            {
    public void saveServiceConfig(NgmaServiceConfig serviceConfig)
        throws PlcmStatusException
    {
        // save transport list
        NgmaServiceConfigMem mem = (NgmaServiceConfigMem) ConvertUtils.convert(serviceConfig,
                NgmaServiceConfigMem.class);
        String lockKey = mem.getKey() + ":Lock";
        BlockableLock lock = new BlockableLock(memoryService, lockKey);
        try
        {
            lock.lock();
            saveServiceConfigInLocked(serviceConfig, mem);
        }
        finally
        {
            lock.unlock();
        }
    }

                String key = iter.next();
                try
                {
                    serviceConfigList.add(getServiceConfigByRedisKey(key));
                }
                catch (Exception e)
                {
                    throw new PlcmStatusException("restore all service config error!", RpumExceptionStatus.EXCEPTION, e);
                }
            }
        }
        return serviceConfigList;
    }
 
   

2.2  Save service config

    public void saveServiceConfig(NgmaServiceConfig serviceConfig)
        throws PlcmStatusException
    {
        // save transport list
        NgmaServiceConfigMem mem = (NgmaServiceConfigMem) ConvertUtils.convert(serviceConfig,
                NgmaServiceConfigMem.class);
        String lockKey = mem.getKey() + ":Lock";
        BlockableLock lock = new BlockableLock(memoryService, lockKey);
        try
        {
            lock.lock();
            saveServiceConfigInLocked(serviceConfig, mem);
        }
        finally
        {
            lock.unlock();
        }
    }

NgmaServiceConfig 通过convert 转换为NgmaServiceConfigMem(适合redis的存储对象),主要是冗余service node ,service type的key


 static
    {
        ConvertUtils.register(new NgmaServiceConfig2MemConverter(), NgmaServiceConfigMem.class);
        ConvertUtils.register(new NgmaServiceConfigFromMemConverter(), NgmaServiceConfig.class);
        ConvertUtils.register(new NgmaServiceType2MemConverter(), NgmaServiceTypeMem.class);
        ConvertUtils.register(new NgmaServiceTypeFromMemConverter(), NgmaServiceType.class);
    }


  private static class NgmaServiceConfig2MemConverter implements Converter
    {
        @Override
        public T convert(Class type, Object value)
        {
            if (type.equals(NgmaServiceConfigMem.class) && value != null
                    && value.getClass().equals(NgmaServiceConfig.class))
            {
                NgmaServiceConfig config = (NgmaServiceConfig) value;
                NgmaServiceConfigMem mem = new NgmaServiceConfigMem();
                mem.setExtendedAttr(config.getExtendedAttr());
                mem.setServiceDefinedTransportListKey(createTransportListKey(config.getUuid()));
                mem.setServiceDefinedTransport(config.getServiceDefinedTransport());
                mem.setServiceName(config.getServiceName());
                mem.setServiceNodeKey(config.getServiceNode().getKey());
                NgmaServiceTypeMem serviceTypeMem = (NgmaServiceTypeMem) ConvertUtils.convert(
                        config.getServiceType(), NgmaServiceTypeMem.class);
                mem.setServiceTypeKey(serviceTypeMem.getKey());
                mem.setUuid(config.getUuid());
                return (T) mem;
            }
            return null;
        }
    }

你可能感兴趣的:(Nosql)