ScopedCacheManager - 범위 지정됨

  • 릴리스 버전: Washingtondc
  • 업데이트 날짜 2024년 02월 01일
  • 읽기15분
  • 범위가 지정된 애플리케이션에 대한 캐시 데이터 검색, 설정 및 플러시를 활성화합니다.

    이 API에는 Scoped Cache(com.glide.scopedcache) 플러그인이 필요하며 sn_scoped_cache 네임스페이스 내에 제공됩니다. 플러그인은 기본적으로 설치되고 활성화됩니다.

    이러한 메서드를 사용하려면 하나 이상의 범위 지정 캐시와 범위 지정 캐시 쌍을 정의해야 합니다. 범위 지정 캐시 만들기에 대한 자세한 내용은 을 참조하십시오.

    범위가 지정된 캐시와 함께 이 API를 사용하면 다음과 같은 몇 가지 이점이 있습니다.
    • 트랜잭션 간 메모리에 있는 범위가 지정된 응용 프로그램의 캐싱 문자열Caching strings in scoped applications that live in memory across transactions.
    • 메모리에 범위가 지정된 애플리케이션 데이터를 캐싱하고 기본 테이블의 변경 내용에 따라 이 데이터를 플러시합니다.
    • 트랜잭션 수명주기 외부에서 캐시 데이터를 정의하고 사용합니다.
    • 나중에 빠르게 액세스할 수 있도록 비용이 많이 드는 작업 또는 계산의 출력을 캐싱합니다.

    ScopedCacheManager – flushScopedCache(문자열 카탈로그, 문자열 키)

    전체 캐시를 플러시하거나 캐시에서 단일 항목을 플러시합니다.

    표 1. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    문자열 옵션입니다. 캐시 키입니다. 이 매개변수를 설정하여 캐시에서 단일 항목을 플러시합니다.
    표 2. 반환
    유형 설명
    없음

    다음 예제에서는 myCache라는 캐시에서 "abc" 키를 플러시하는 방법을 보여 줍니다. 전체 캐시를 플러시하려면 key 매개 변수를 생략합니다.

    sn_scoped_cache.ScopedCacheManager.flushScopedCache("myCache", "abc");

    ScopedCacheManager – get(문자열 카탈로그, 문자열 키)

    캐시에서 값을 가져옵니다.

    표 3. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    문자열 캐시 키입니다.
    표 4. 반환
    유형 설명
    문자열 캐시 항목입니다.

    다음 예에서는 키(50)보다 큰 문자로 짧은 설명이 있는 인시던트 기록의 수를 계산하는 방법을 보여줍니다. 이 경우 50자를 넘는 짧은 설명이 포함된 인시던트 기록이 6개 있습니다. 이 값은 다시 사용할 수 있는 문자열로 캐시에 저장되므로 값을 검색할 때마다 계산할 필요가 없습니다.

    var cache2 = "Cache 2"; // column pair type
    
    // Re-populate the column cache
    for (var i = 0; i < 100; i+=10) {
        var inc = new GlideRecord('incident');
        inc.query();
        var counter = 0;
        while (inc.next()) {
            if (inc.getValue('short_description').length > i)
            counter++;
        }
        sn_scoped_cache.ScopedCacheManager.put(cache2, i.toString(), counter.toString());
    }
    
    // script to get values from column cache
    gs.info(sn_scoped_cache.ScopedCacheManager.get(cache2, "50"));

    출력:

    scoped_app: 6

    ScopedCacheManager – getCacheEntryDetails(문자열 카탈로그, 문자열 키)

    바이트 크기, 객체 유형 및 항목 값을 포함한 캐시 항목 상세 정보를 가져옵니다.

    표 5. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.

    카탈로그 이름에는 범위 이름과 캐럿 문자가 프리픽스로 와야 합니다. 예를 들어 전역 범위의 &quot;test_cache&quot;에는 접두사 &quot;global^test_cache&quot;이 붙습니다.

    문자열 캐시 키입니다.
    표 6. 반환
    유형 설명
    문자열 &lt;ObjectType> (&lt;EntrySize>) 형식의 문자열: &lt;Value.toString()>.

    다음 예제에서는 test_cache라는 범위 지정 캐시에 대한 캐시 항목 세부 정보를 표시하는 방법을 보여 줍니다.

    sn_scoped_cache.ScopedCacheManager.put("test_cache", "key1", "val1");
    gs.info(sn_scoped_cache.ScopedCacheManager.getCacheEntryDetails("global^test_cache", "key1"));

    출력:

    java.lang.String (48):
    val1

    ScopedCacheManager – getScopedCacheKeys(카탈로그 문자열)

    범위가 지정된 캐시에서 순차적 키 목록을 가져옵니다.

    표 7. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.

    카탈로그 이름에는 범위 이름과 캐럿 문자가 프리픽스로 와야 합니다. 예를 들어 전역 범위의 &quot;test_cache&quot;에는 접두사 &quot;global^test_cache&quot;이 붙습니다.

    표 8. 반환
    유형 설명
    배열 지정된 캐시의 각 키를 순차적으로 나열하는 반복기 배열입니다.

    다음 예제에서는 test_cache라는 캐시에 있는 각 키의 값을 인쇄하는 방법을 보여 줍니다.

    sn_scoped_cache.ScopedCacheManager.put("test_cache", "key1", "val1");
    sn_scoped_cache.ScopedCacheManager.put("test_cache", "key2", "val2");
    
    var iter = sn_scoped_cache.ScopedCacheManager.getScopedCacheKeys("global^test_cache");
    while (iter.hasNext()) {
        gs.info(iter.next());
    }

    출력:

    *** Script: key1
    *** Script: key2

    ScopedCacheManager – prefixFlush(문자열 카탈로그, 문자열 접두사)

    지정된 접두사로 시작하는 키가 있는 모든 캐시 항목을 플러시합니다.

    표 9. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    프리픽스 문자열 캐시를 플러시할 때 키를 검색할 접두사입니다.
    표 10. 반환
    유형 설명
    없음

    다음 예제에서는 접두사 a가 있는 키의 캐시 값을 플러시하는 방법을 보여 줍니다.

    sn_scoped_cache.ScopedCacheManager.put("test_cache", "aKey1", "val1");
    sn_scoped_cache.ScopedCacheManager.put("test_cache", "aKey2", "val2");
    sn_scoped_cache.ScopedCacheManager.put("test_cache", "bKey1", "val3");
    
    sn_scoped_cache.ScopedCacheManager.prefixFlush("test_cache", "a");
    
    gs.info(sn_scoped_cache.ScopedCacheManager.get("test_cache", "aKey1"));
    gs.info(sn_scoped_cache.ScopedCacheManager.get("test_cache", "aKey2"));
    gs.info(sn_scoped_cache.ScopedCacheManager.get("test_cache", "bKey1"));

    출력:

    *** Script: null
    *** Script: null
    *** Script: val3

    ScopedCacheManager – put(문자열 카탈로그, 문자열 키, 문자열 값)

    테이블 또는 테이블 열 쌍 유형인 캐시의 범위가 지정된 캐시에 값을 넣습니다.

    표 11. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    문자열 캐시 키입니다.
    문자열 캐시 항목입니다.
    표 12. 반환
    유형 설명
    없음

    다음 예제에서는 &quot;abc&quot;라는 키를 정의하고 myCache라는 캐시로 검색하는 방법을 보여 줍니다. 캐시는 인시던트 [incident] 테이블과 테이블 쌍을 이룹니다. 이 예는 인시던트 기록의 설명 필드를 업데이트한 후 캐시가 플러시되는 방식도 보여줍니다.

    //put value in cache for key "abc"
    gs.info("put key 'abc' into cache with value 'value1'");
    sn_scoped_cache.ScopedCacheManager.put("myCache", "abc", "value1");
    
    // retrieve value from cache for key "abc"
    gs.info("value of 'abc' in cache: ");
    gs.info(sn_scoped_cache.ScopedCacheManager.get("myCache", "abc")+"\n");
    
    //update an incident record (add string to the description field)
    gs.info("Updating incident record.");
    var gr = new GlideRecord("incident");
    gr.get("<incident_sys_id>");
    gr.description= "test12345";
    if (gr.update()) {
        gs.info("Record updated successfully.\n");
    };
    
    //retrieve the value from cache for key "abc"
    gs.info("Retrieving 'abc' from cache. Value should be null because a table pair was updated.");
    gs.info(sn_scoped_cache.ScopedCacheManager.get("myCache", "abc"));

    출력:

    scoped_app: put key 'abc' into cache with value 'value1'
    scoped_app: value of 'abc' in cache: 
    scoped_app: value1
    
    scoped_app: Updating incident record.
    scoped_app: Record updated successfully.
    
    scoped_app: Retrieving 'abc' from cache. Value should be null because a table pair was updated.
    scoped_app: null

    ScopedCacheManager – putMultiRow(문자열 카탈로그, 문자열 키, 문자열 값, 배열 ID)

    항목을 캐시에 넣고 지정된 sys_ids의 모든 레코드를 해당 항목의 플러시 소스로 설정합니다. 이 메서드는 테이블 또는 테이블 열 쌍 형식의 캐시 를 위한 것입니다.

    표 13. 매개변수
    이름 유형 설명
    카탈로그 문자열 플러시할 카탈로그입니다. 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    문자열 새 항목의 캐시 키입니다.
    문자열 새 항목에 대한 값입니다.
    id 배열 항목의 플러시 소스로 사용할 기록의 sys_ids 목록입니다. 이러한 기록은 서로 다른 테이블에서 온 것일 수 있습니다.
    표 14. 반환
    유형 설명
    없음

    다음 예제에서는 인시던트 및 사용자 기록을 플러시 포인트로 사용하는 방법을 보여줍니다. 지정된 기록 중 하나가 업데이트되면 test_cache 키 INC0000060 플러시됩니다.

    // This example requires a table-row pair for the "incident" and "sys_user" tables assigned to a cache named test_cache
    var inc1ID = "1c741bd70b2322007518478d83673af3";
    var userID = "681ccaf9c0a8016400b98a06818d57c7";
    
    // update to email should invalidate the entry
    gs.info("initial put");
    putCacheEntry();
    gs.info("cache value for INC0060: " + sn_scoped_cache.ScopedCacheManager.get("test_cache", "INC0000060"));
    var gr = new GlideRecord("sys_user");
    gr.get(userID);
    gr.setValue("email", "a" + gr.getValue("email"));
    gr.update();
    gs.info("updated user email");
    gs.info("cache value: " + sn_scoped_cache.ScopedCacheManager.get("test_cache", "INC0000060"));
    
    // update to incident number should invalidate the entry.
    gs.info("second put");
    putCacheEntry();
    gs.info("cache value: " + sn_scoped_cache.ScopedCacheManager.get("test_cache", "INC0000060"));
    var gr = new GlideRecord("incident");
    gr.get(inc1ID);
    gr.setValue("number", gr.getValue("number") + "1");
    gr.update();
    gs.info("updated incident number");
    gs.info("cache value: " + sn_scoped_cache.ScopedCacheManager.get("test_cache", "INC0000060"));
    
    function putCacheEntry() {
        var incGr = new GlideRecord("incident");
        incGr.get(inc1ID);
        var userGr = new GlideRecord("sys_user");
        userGr.get(userID);
        // the cache stores that the incident is assigned to a specific user. So if the incident or user record changes, we should flush.
        sn_scoped_cache.ScopedCacheManager.putMultiRow("test_cache",incGr.getValue("number"), userGr.getValue("email"), [inc1ID, userID]);
    }

    출력:

    *** Script: initial put
    *** Script: cache value for INC0060: employee@example.com
    *** Script: Updating Joe Employee's primary email device based on change to user record email address
    *** Script: updated user email
    *** Script: cache value: null
    *** Script: second put
    *** Script: cache value: aemployee@example.com
    *** Script: updated incident number
    *** Script: cache value: null

    ScopedCacheManager – putRow(문자열 카탈로그, 문자열 키, 문자열 값, 문자열 sysId)

    캐시의 단일 행(테이블)에 값을 넣습니다. 테이블 행 또는 테이블 행 및 열 유형의 캐시에 이 메서드를 사용합니다.

    표 15. 매개변수
    이름 유형 설명
    카탈로그 문자열 범위 지정 캐시 [sys_scoped_cache] 테이블에 정의된 캐시의 이름입니다.
    문자열 캐시 키입니다.
    문자열 캐시 항목입니다.
    sysId 문자열 옵션입니다. 테이블의 행 Sys_id, 즉 추적 중인 기록입니다.
    표 16. 반환
    유형 설명
    없음

    다음 예에서는 incident_cache라는 테이블 행 쌍 캐시 유형을 사용합니다. 캐시는 인시던트 호출자 ID를 추가하고 변경된 레코드의 sys_id 연결된 캐시 항목이 지워집니다.

    // build the cache:
    var number = 'INC0009009';
    var inc = new GlideRecord('incident');
    inc.addQuery('number', number);
    inc.query();
    inc.next();
    var entry = inc.caller_id.getDisplayValue();
    sn_scoped_cache.ScopedCacheManager.putRow("incident_cache", inc.getValue('number'), entry, inc.getValue('sys_id'));
    gs.info(sn_scoped_cache.ScopedCacheManager.get("incident_cache", number));
    
    // when the incident is altered, the cache entry will be flushed.
    inc = new GlideRecord('incident');
    inc.addQuery('number', number);
    inc.query();
    inc.next();
    inc.setValue('short_description', inc.getValue('short_description') + "a");
    inc.update();
    gs.info(sn_scoped_cache.ScopedCacheManager.get("incident_cache", number));

    출력:

    scoped_app: John Jones
    scoped_app: null