Coder Social home page Coder Social logo

objectcache's Introduction

Actions Status

NAME

ObjectCache - A role to cache objects

SYNOPSIS

use ObjectCache;

sub id(%h --> Int:D) {
    %h<id> or die "must have an id";
}

class Article does ObjectCache[&id] {
    has $.id;
    # has many more attributes
}

say Article.new(id => 42666789).WHICH;  # Article|42666789

DESCRIPTION

The ObjectCache role mixes the logic of creating a proper ObjectType class and making sure that each unique ObjectType only exists once in memory, into a class.

The role takes a Callable parameterization to indicate how a unique ID should be created from the parameters given (as a hash) with the call to the new method. You can also adapt the given hash making sure any default values are properly applied. If there's already an object created with the returned ID, then no new object will be created but the one from the cache will be returned.

A class is considered to be an object type if the .WHICH method returns an object of the ObjAt class.

This is specifically important when using set operators (such as (elem), or Sets, Bags or Mixes, or any other functionality that is based on the === operator functionality, such as unique and squish.

The format of the value that is being returned by WHICH is only valid during a run of a process. So it should not be stored in any permanent medium.

Removing objects from cache

The ObjectCache role contains a private method !EVICT. If you'd like to have the ability to remove an object from the cache, you should create a method in your class that will call this method:

class Article does ObjectCache[&id] {
    method remove-from-cache() { self!EVICT }
    # more stuff
}

The !EVICT method returns the object that is removed from the cache, or Nil if the object was not in the cache.

Clearing the cache completely

The ObjectCache role contains a private method !CLEAR. If you'd like to have the ability to cleare the cache completely, you should create a method in your class that will call this method:

class Article does ObjectCache[&id] {
    method clear-cache() { self!CLEAR }
    # more stuff
}

The !CLEAR method returns the number of objects that have been removed from the cache.

AUTHOR

Elizabeth Mattijsen [email protected]

Source can be located at: https://github.com/lizmat/ObjectCache . Comments and Pull Requests are welcome.

If you like this module, or what Iโ€™m doing more generally, committing to a small sponsorship would mean a great deal to me!

COPYRIGHT AND LICENSE

Copyright 2020, 2021, 2024 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

objectcache's People

Contributors

lizmat avatar

Stargazers

 avatar

Watchers

James Cloos avatar

objectcache's Issues

Choose constructor?

In line with my REST use case from #1, I wonder if I could wrap fetch instead of new, essentially memoizing a factory method?

I don't mind refactoring to use new to construct the objects, and do a fetch from the constructor, but that seems like I would be overloading my constructor in a way I'm not sure I like.

Clear cache?

Another lizmat module that does exactly what I was trying to figure out how to do!

The documentation does not suggest a way to remove an object from cache. I was going to use it to avoid unnecessarily fetching an API object I already have, but I would like to be able to drop my cached data so I can re-fetch it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.