Python django.core.cache.cache.set() Examples

The following are 30 code examples of django.core.cache.cache.set(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.core.cache.cache , or try the search function .
Example #1
Source File: api.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def get(self, request, graph_id=None):
        perm = "read_nodegroup"
        datatypes = models.DDataType.objects.all()
        graph = cache.get(f"graph_{graph_id}")
        user = request.user
        if graph is None:
            print("not using graph cache")
            graph = Graph.objects.get(graphid=graph_id)
            cache.set(f"graph_{graph_id}", JSONSerializer().serializeToPython(graph), settings.GRAPH_MODEL_CACHE_TIMEOUT)
        cards = CardProxyModel.objects.filter(graph_id=graph_id).order_by("sortorder")
        permitted_cards = []
        for card in cards:
            if user.has_perm(perm, card.nodegroup):
                card.filter_by_perm(user, perm)
                permitted_cards.append(card)
        cardwidgets = [
            widget for widgets in [card.cardxnodexwidget_set.order_by("sortorder").all() for card in permitted_cards] for widget in widgets
        ]

        return JSONResponse({"datatypes": datatypes, "cards": permitted_cards, "graph": graph, "cardwidgets": cardwidgets}) 
Example #2
Source File: queries.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def cache_by_args(func, seconds=38800): # 8 hours by default
    """
    Decorator to cache query results from SIMS (if successful: no SIMSProblem).
    Requires arguments that can be converted to strings that uniquely identifies the results.
    Return results must be pickle-able so they can be cached.
    """
    def wrapped(*args, **kwargs):
        key = "simscache-" + func.__name__ + "-" + _args_to_key(args, kwargs)
        # first check cache
        cacheres = cache.get(key)
        if cacheres:
            return cacheres
        
        # not in cache: calculate
        res = func(*args, **kwargs)

        # got a result (with no exception thrown): cache it
        cache.set(key, res, seconds)
        return res
    
    wrapped.__name__ = func.__name__
    return wrapped 
Example #3
Source File: annotations.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def annotate(nodes, tree=None):
    """
    Args:
        nodes: list lxml element tree nodes with lazy tree instance
    Returns:
        Annotated nodes with attribute and value specified in annotation file
    """
    if not tree:
        tree = get_annotation_tree()

    if tree and nodes:
        for node in nodes:
            xpath = node.get('path', '')
            instance = tree.search(xpath)
            if not instance:
                continue
            for attr, value in instance.attrib.items():
                node.set(attr, value)
            annotate(list(node), tree)
    return nodes 
Example #4
Source File: annotations.py    From yang-explorer with Apache License 2.0 6 votes vote down vote up
def get_annotation_tree():
    ann_path = ServerSettings.annotation_path(None)
    filename = os.path.join(ann_path, 'covered.json')
    if not os.path.exists(filename):
        return None

    tree = cache.get('ui_ann', None)
    if tree is None:
        tree = XPathTree('/', None)
        with open(filename, 'r') as f:
            profile = json.load(f)
            for line in profile.get('data', []):
                tree.insert(line, profile.get('annotate', None))
            cache.set('ui_ann', tree)
    else:
        print 'From cache..'
    return tree 
Example #5
Source File: mock.py    From controller with MIT License 6 votes vote down vote up
def remove_cache_item(url, resource_type):
    # remove data object from individual cache
    cache.delete(url)
    # get rid of log element as well for pods
    if resource_type == 'pod':
        cache.delete(url + '_log')

    # remove from the resource type global scope
    items = cache.get(resource_type, [])
    if url in items:
        items.remove(url)
        cache.set(resource_type, items, None)

    # remove from namespace specific scope
    # sneaky way of getting data up to the resource type without too much magic
    cache_url = ''.join(url.partition(resource_type)[0:2])
    items = cache.get(cache_url, [])
    if url in items:
        items.remove(url)
    cache.set(cache_url, items, None) 
Example #6
Source File: mock.py    From controller with MIT License 6 votes vote down vote up
def add_cache_item(url, resource_type, data):
    cache.set(url, data, None)

    # Keep track of what resources are in a given resource
    items = cache.get(resource_type, [])
    if url not in items:
        items.append(url)
        cache.set(resource_type, items, None)

    # Keep track of what resources exist under other resources (mostly namespace)
    # sneaky way of getting data up to the resource type without too much magic
    cache_url = ''.join(url.partition(resource_type)[0:2])
    items = cache.get(cache_url, [])
    if url not in items:
        items.append(url)
        cache.set(cache_url, items, None) 
Example #7
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def save(self, *args, **kwargs):
        # maintain self.series as identifying the category of requirements across programs in this unit    
        with django.db.transaction.atomic():
            if not self.series:
                others = GradRequirement.objects \
                        .filter(description=self.description,
                                program__unit=self.program.unit)
                if others:
                    # use the series from an identically-named requirement
                    ser = others[0].series
                else:
                    # need a new series id
                    used = set(r.series for r in GradRequirement.objects.all())
                    try:
                        ser = max(used) + 1
                    except ValueError:
                        ser = 1
                
                self.series = ser
            
            super(GradRequirement, self).save(*args, **kwargs) 
Example #8
Source File: photos.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def do_photo_fetch(emplids):
    """
    Do the actual work of fetching photos: called by the celery task. Store them in the cache for finding by the view.
    """
    photos = _get_photos(emplids)
    for emplid in photos:
        cache.set(_photo_cache_key(emplid), photos[emplid], 3600*24)

    missing = set(emplids) - set(photos.keys())
    if missing:
        # some images missing: cache the failure, but not for as long
        data = open(DUMMY_IMAGE_FILE, 'rb').read()
        for emplid in missing:
            cache.set(_photo_cache_key(emplid), data, 3600)

    result = list(set(photos.keys()))
    logger.debug("do_photo_fetch(%r) returning %r" % (emplids, result))
    return result



# functions that actually get photos 
Example #9
Source File: queries.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def escape_arg(self, a):
        """
        Escape argument for DB2
        """
        # Based on description of PHP's db2_escape_string
        if type(a) in (int,int):
            return str(a)
        if type(a) in (tuple, list, set):
            return '(' + ', '.join((self.escape_arg(v) for v in a)) + ')'
        
        # assume it's a string if we don't know any better
        a = str(a)
        a = a.replace("\\", "\\\\")
        a = a.replace("'", "\\'")
        a = a.replace('"', '\\"')
        a = a.replace("\r", "\\r")
        a = a.replace("\n", "\\n")
        a = a.replace("\x00", "\\\x00")
        a = a.replace("\x1a", "\\\x1a")
        return "'" + a + "'" 
Example #10
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def get_wikitext(self):
        """
        Return this version's markup (reconstructing from diffs if necessary).
        
        Caches when reconstructing from diffs
        """
        if self.diff_from:
            key = self.wikitext_cache_key()
            wikitext = cache.get(key)
            if wikitext:
                return str(wikitext)
            else:
                src = self.diff_from
                diff = json.loads(self.diff)
                wikitext = src.apply_changes(diff)
                cache.set(key, wikitext, 24*3600) # no need to expire: shouldn't change for a version
                return str(wikitext)

        return str(self.wikitext) 
Example #11
Source File: mock.py    From controller with MIT License 6 votes vote down vote up
def add_cleanup_pod(url):
    """populate the cleanup pod list"""
    # variance allows a pod to stay alive past grace period
    variance = random.uniform(0.1, 1.5)
    grace = round(settings.KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS * variance)

    # save
    pods = cache.get('cleanup_pods', {})
    pods[url] = (datetime.utcnow() + timedelta(seconds=grace))
    cache.set('cleanup_pods', pods)

    # add grace period timestamp
    pod = cache.get(url)
    grace = settings.KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS
    pd = datetime.utcnow() + timedelta(seconds=grace)
    timestamp = str(pd.strftime(MockSchedulerClient.DATETIME_FORMAT))
    pod['metadata']['deletionTimestamp'] = timestamp
    cache.set(url, pod) 
Example #12
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def program_as_of(self, semester=None, future_if_necessary=False):
        if semester == None:
            semester = Semester.current()

        gph = GradProgramHistory.objects.filter(student=self, start_semester__name__lte=semester.name) \
            .order_by('-start_semester', '-starting').select_related('program').first()

        if not gph and future_if_necessary:
            # look into the future for the program the *will* be in: that's how we'll set gs.program earlier.
            gph = GradProgramHistory.objects.filter(student=self) \
            .order_by('start_semester', '-starting').select_related('program').first()

        if gph:
            return gph.program
        else:
            return None 
Example #13
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def save(self, *args, **kwargs):
        super(TAContract, self).save(*args, **kwargs)

        # set SIN field on any GradStudent objects for this person
        from grad.models import GradStudent
        for gs in GradStudent.objects.filter(person=self.application.person):
            dummy_sins = ['999999999', '000000000', '123456789']
            if (('sin' not in gs.config 
                or ('sin' in gs.config and gs.config['sin'] in dummy_sins)) 
                and not self.sin in dummy_sins ):
                gs.person.set_sin(self.sin)
                gs.person.save()

        from tacontracts.models import TAContract as NewTAContract
        NewTAContract.update_ta_members(self.application.person, self.posting.semester_id)

        # If the status of this contract is Cancelled or Rejected, find all the TACourses
        # it applies to and set their BUs to 0.
        if self.status in ('CAN', 'REJ'):
            courses = TACourse.objects.filter(contract=self)
            for course in courses:
                course.bu = 0
                course.save() 
Example #14
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def html_contents(self, offering=None):
        """
        Return the HTML version of this version's wikitext (with macros substituted if available)

        offering argument only required if self.page isn't set: used when doing a speculative conversion of unsaved content.
        
        Cached to save frequent conversion.
        """
        key = self.html_cache_key()
        html = cache.get(key)
        if html:
            return mark_safe(html)
        else:
            markup_content = self.substitute_macros(self.get_wikitext())
            html = markup_to_html(markup_content, self.markup(), pageversion=self, html_already_safe=True)
            cache.set(key, html, 24*3600) # expired if activities are changed (in signal below), or by saving a PageVersion in this offering
            return mark_safe(html)


# signal for cache invalidation 
Example #15
Source File: card.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def save(self):
        """
        Saves a card and its parent ontology property back to the db

        """
        with transaction.atomic():
            if self.graph.ontology and self.graph.isresource:
                edge = self.get_edge_to_parent()
                if self.ontologyproperty is not None:
                    edge.ontologyproperty = self.ontologyproperty
                edge.save()

            self.nodegroup.cardinality = self.cardinality
            self.nodegroup.save()

            super(Card, self).save()
            for widget in self.widgets:
                widget.save()
            for node in self.nodes:
                node.save()
            for card in self.cards:
                card.save()
            cache.set(f"card_{self.cardid}", JSONSerializer().serializeToPython(self))

        return self 
Example #16
Source File: importer.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def import_joint(extra_where='1=1'):
    """
    Find combined sections and set CourseOffering.config['joint_with'] appropriately.
    """
    db = SIMSConn()
    db.execute("SELECT strm, class_nbr, sctn_combined_id FROM ps_sctn_cmbnd c WHERE c.strm IN %s "
               " AND ("+extra_where+")", (import_semesters(),))

    for k,v in itertools.groupby(db, lambda d: (d[0], d[2])):
        # for each combined offering...
        strm, _ = k
        class_nbrs = [int(class_nbr) for _,class_nbr,_ in v]
        offerings = CourseOffering.objects.filter(semester__name=strm, class_nbr__in=class_nbrs)
        for offering in offerings:
            offering.set_joint_with([o.slug for o in offerings if o != offering])
            offering.save() 
Example #17
Source File: flags.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def disabled_features():
    """
    Get the current set of disabled features: from the Django cache if possible, or from the loader if not.
    """
    if cache_timeout:
        # check the cache and return it if we find it
        flags = cache.get(cache_key)
        if flags is not None:
            return flags

    # not in the cache: have the loader find it
    loader = get_loader()
    flags = loader.get_disabled_features()
    if cache_timeout:
        cache.set(cache_key, flags, cache_timeout)

    return flags 
Example #18
Source File: middleware.py    From django-rest-messaging with ISC License 6 votes vote down vote up
def process_view(self, request, callback, callback_args, callback_kwargs):

        assert hasattr(request, 'user'), (
            "The django-rest-messaging MessagingMiddleware requires an authentication middleware "
            "to be installed because request.user must be available."
        )

        if request.user.is_authenticated():

            participant = cache.get('rest_messaging_participant_{0}'.format(request.user.id), None)

            if participant is None:
                # either we create the participant or we retrieve him
                try:
                    participant = Participant.objects.get(id=request.user.id)
                except:
                    participant = Participant.objects.create(id=request.user.id)
                cache.set('rest_messaging_participant_{0}'.format(request.user.id), participant, 60 * 60)  # cached for 60 minutes
        else:
            participant = None

        request.rest_messaging_participant = participant

        return None 
Example #19
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def super_units(self, include_self=False):
        """
        Units directly above this in the heirarchy
        """
        key = 'superunits-' + self.slug
        res = cache.get(key)
        if res:
            if include_self:
                return res + [self]
            else:
                return res
        else:
            res = self.__super_units()
            cache.set(key, res, 24*3600)
            if include_self:
                return res + [self]
            else:
                return res 
Example #20
Source File: device.py    From Servo with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def model_from_slug(product_line, model=None):
    """
    Returns product description for model slug or models dict for
    the specified product line
    """
    if not cache.get("slugmap"):
        slugmap = {}  # Map model slug to corresponding product description
        product_lines = gsxws.products.models()

        for k, v in product_lines.items():
            d = {}
            for p in v['models']:
                slug = slugify(p)
                d[slug] = p

            slugmap[k] = d

        cache.set("slugmap", slugmap)

    models = cache.get("slugmap").get(product_line)

    if model is not None:
        return models.get(model)

    return models 
Example #21
Source File: account.py    From AnsibleUI with GNU General Public License v3.0 6 votes vote down vote up
def notes(request, *k, **kw):
    dataKey = request.GET.get('dataKey')
    data = cache.get("note_%s" % dataKey)
    if not data:
        if dataKey:
            fl = '%s/%s' % (note_base_dirt, dataKey)
            if os.path.isfile(fl):
                with open(fl)as f:
                    s = f.read()
            else: s = "错误❎"
            files = []
        else:
            files = os.listdir(note_base_dirt)
            s = ""
        data = {"msg": s, "files": files}
        cache.set("note_%s" % dataKey, data)
    else: print("Cache: %s" % dataKey)
    return render(request, "notes.html", data) 
Example #22
Source File: lichessapi.py    From heltour with MIT License 5 votes vote down vote up
def _apicall(url, timeout=300, check_interval=0.1, post_data=None):
    # Make a request to the local API worker to put the result of a lichess API call into the redis cache
    if post_data:
        r = requests.post(url, data=post_data)
    else:
        r = requests.get(url)
    if r.status_code != 200:
        # Retry once
        if post_data:
            r = requests.post(url, data=post_data)
        else:
            r = requests.get(url)
        if r.status_code != 200:
            raise ApiWorkerError('API worker returned HTTP %s for %s' % (r.status_code, url))
    # This is the key we'll use to obtain the result, which may not be set yet
    redis_key = r.text

    # Wait until the result is set in redis (with a timeout)
    time_spent = 0
    while True:
        result = cache.get(redis_key)
        if result is not None:
            return result
        time.sleep(check_interval)
        time_spent += check_interval
        if time_spent >= timeout:
            raise ApiWorkerError('Timeout for %s' % url) 
Example #23
Source File: panic.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def handle(self, *args, **kwargs):
        timeout = settings.FEATUREFLAGS_PANIC_TIMEOUT
        flags = settings.FEATUREFLAGS_PANIC_DISABLE

        if not flags:
            self.stdout.write(
                "You have not listed any disable-when-panicked features in settings.FEATUREFLAGS_PANIC_DISABLE. \n"
                "Nothing to disable here. Please continue to panic.")
            return

        cache.set(cache_key, flags, timeout)
        self.stdout.write("These features have been disabled for %s seconds:\n" % (timeout))
        for f in flags:
            self.stdout.write("  " + f)
        self.stdout.write("Best of luck. You may return to normal operation by calling 'manage.py unpanic'.\n") 
Example #24
Source File: uptime.py    From heltour with MIT License 5 votes vote down vote up
def is_up(self, value):
        cache.set(self.name, value, self.ping_interval.total_seconds()) 
Example #25
Source File: models.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def sub_unit_ids(cls, units, by_id=False):
        """
        Get the Unit.id values for all descendants of the given list of unit.id values.

        Cached so we can avoid the work when possible.
        """
        # sort unit.id values for consistent cache keys
        if by_id:
            unitids = sorted(list(set(units)))
        else:
            unitids = sorted(list(set(u.id for u in units)))
        return Unit.__sub_unit_ids(unitids) 
Example #26
Source File: check_things.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def handle(self, *args, **options):
        if options['cache_subcall']:
            # add one to the cached value, so the main process can tell we see/update the same cache
            res = cache.get('check_things_cache_test', -100)
            cache.set('check_things_cache_test', res + 1)
            return

        info = panel.settings_info()
        passed, failed = panel.deploy_checks()
        unknown = []

        # email sending
        if options['email']:
            email = options['email']
            success, result = panel.send_test_email(email)
            if success:
                unknown.append(('Email sending', result))
            else:
                failed.append(('Email sending', result))
        else:
            unknown.append(('Email sending', "provide an --email argument to test."))

        # report results
        self._report('For information', info)
        self._report('These checks passed', passed)
        self._report('These checks failed', failed)
        self._report('Status unknown', unknown)
        self.stdout.write('\n') 
Example #27
Source File: importer.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def has_letter_activities(offering):
    key = 'has-letter-' + offering.slug
    res = cache.get(key)
    if res is not None:
        return res
    else:
        las = LetterActivity.objects.filter(offering=offering, deleted=False)
        res = las.count() > 0
        cache.set(key, res, 12*60*60) 
Example #28
Source File: importer.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def get_role_people():
    """
    Force a get_person() on all of those with roles and RA appointments
    """
    roles = Role.objects_fresh.all().select_related('person')
    people = set(r.person for r in roles)

    cutoff = datetime.datetime.now() - datetime.timedelta(days=365)
    ras = RAAppointment.objects.filter(start_date__lte=cutoff).select_related('person')
    people |= set([ra.person for ra in ras])

    for p in people:
        get_person(p.emplid, grad_data=True) 
Example #29
Source File: models.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def all_roles(cls, userid):
        return set((r.role for r in Role.objects_fresh.filter(person__userid=userid))) 
Example #30
Source File: checkin.py    From Servo with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def set_cache_device(device):
    key = 'checkin-device-%s' % device.sn
    cache.set(key, device)