AbapGit
A source-verified project description is not available yet. Open the detail page or wait for a manual refresh.
Source facts need a manual refresh
ABAP Dynamic Cache Box Generator
ABAP Dynamic Cache Box Generator
This repository provides a flexible and reusable ABAP cache implementation using macros. The cache classes are dynamically generated based on the given class name and data type, making it easy to create caches for various data types without duplicating code.
The provided macros allow you to generate cache classes for different data types dynamically. This approach avoids code duplication and simplifies the process of creating cache classes for various use cases. The cache implementation uses the Singleton pattern to ensure a single instance of each cache class and employs hashed tables for efficient data access.
The cache classes are generated using macros. You can create a cache class for any data type by specifying the class name and data type in the macro. The class will be named using the provided class name with the suffix CACHEBOX.
Generates a cache class for a value type.
### Syntax:
``abap generatedatacachebox: . ``
### Example:
``abap generatedatacachebox: scarr scarr. ``
This will generate a class named zclscarrcachebox with a cache for SCARR data.
Generates a cache class for object references.
### Syntax:
``abap generateorefcachebox: . ``
### Example:
``abap generateorefcachebox: salv clsalvtable. ``
This will generate a class named zclsalvcachebox with a cache for clsalvtable object references.
To generate a cache for a specific value type, use the generatedatacachebox macro. Here is an example of using it for the SCARR table.
REPORT sy-repid.
INCLUDE zcache_box_i_gen. "Include the macro generator
INITIALIZATION.
generate_data_cache_box: scarr scarr. " Cache for SCARR data
START-OF-SELECTION.
" Retrieve the singleton instance of the SCARR cache
DATA(lo_data_cache) = zcl_scarr_cache_box=>get_instance( ).
" Fetch data from SCARR table
SELECT * FROM scarr INTO TABLE @DATA(lt_scarr).
" Populate the cache with SCARR data
LOOP AT lt_scarr INTO DATA(ls_scarr).
lo_data_cache->put(
EXPORTING
key = CONV #( ls_scarr-carrid )
value = ls_scarr
).
ENDLOOP.
" Retrieve an entry from the cache
lo_data_cache->get(
EXPORTING
key = 'AA'
IMPORTING
value = DATA(ls_scarr_aa)
EXCEPTIONS
entry_not_found = 1
OTHERS = 2
).
IF sy-subrc EQ 0.
WRITE: / 'SCARR data for AA:', ls_scarr_aa-carrid, ls_scarr_aa-carrname.
ELSE.
WRITE: / 'Entry Not Found SCARR data for AA'.
ENDIF.
" Display the size of the cache
DATA(cache_size) = lo_data_cache->size( ).
WRITE: / 'Cache size:', cache_size.
" Delete an entry from the cache
lo_data_cache->delete(
EXPORTING
key = 'AA'
EXCEPTIONS
entry_not_found = 1
OTHERS = 2
).
IF sy-subrc EQ 0.
WRITE: / 'Entry with key AA deleted from cache.'.
ELSE.
WRITE: / 'Entry Not Found SCARR data for AA'.
ENDIF.
" Clear all entries from the cache
lo_data_cache->clear( ).
WRITE: / 'Cache cleared.'.To generate a cache for object references, use the generateorefcachebox macro. Here is an example of using it for clsalvtable objects.
REPORT sy-repid.
INCLUDE zcache_box_i_gen. "Include the macro generator
INITIALIZATION.
generate_oref_cache_box: salv cl_salv_table. " Cache for object references
START-OF-SELECTION.
" Retrieve the singleton instance of the reference cache
DATA(lo_oref_cache) = zcl_salv_cache_box=>get_instance( ).
SELECT * FROM scarr INTO TABLE @DATA(lt_scarr).
" Create an ALV table
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lo_salv)
CHANGING
t_table = lt_scarr
).
" Add the ALV table object to the cache
lo_oref_cache->put(
EXPORTING
key = 'SALV'
value = lo_salv
).
" Retrieve the ALV table object from the cache
lo_oref_cache->get(
EXPORTING
key = 'SALV'
IMPORTING
value = DATA(lo_salv2)
EXCEPTIONS
entry_not_found = 1
OTHERS = 2
).
IF sy-subrc EQ 0.
" Display the ALV table
lo_salv2->display( ).
ELSE.
WRITE : / 'Entry Not Found'.
ENDIF.This project is licensed under the MIT License - see the LICENSE file for details.
Contributions to improve the functionality and performance of these cache classes are welcome. Please submit issues or pull requests for any enhancements or bug fixes.